반응형

<script type="text/javascript">


var num = 3.14;


// 그대로 출력

document.write(num, '<br />');

// 출력 결과: 3.14


// 소수점 이하 6자리로 강제로 출력

document.write(num.toFixed(6), '<br />');

// 출력 결과: 3.140000





num = 3.1415926535897932384626433832795;


// 그대로 출력

document.write(num, '<br />');

// 출력 결과: 3.141592653589793


// 소수점 이하 3자리로 출력

document.write(num.toFixed(3), '<br />');

// 출력 결과 (반올림됨): 3.142



</script>


※출처 : http://mwultong.blogspot.com/2007/08/javascript-tofixed.html


자바스크립트에서 숫자가 0.02555555555555555555555555555555와 같이 나올때

이숫자를 0.03으로 변경할때 사용한다.

숫자가 지정된 변수에 val.toFixed(2);로 사용한다.

+ Recent posts