Recent posts

Recent comments

Archive

Calender

«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

Math.floor() : 소수점 이하를 버림한다.
Math.ceil() : 소수점 이하를 올림한다.
Math.round() : 소수점 이하를 반올림한다.


round의 사전적 의미.
1. (숫자를) 반올림하다. (끝수를) 사사오입하다.
2. 주어진 수를 가장 가까운 정수로 반올림하다. Convenient for rounding other numbers to.


Math.round()의 기본적인 작성법은 아래와 같다.

Math.round( 12.34 ); 

또는 

Math[ "round" ]( 12.34 ); 


<button onclick="testing()"> 클릭하세요 </button>

<pre id="round1"> </pre>


<script type="text/javascript">

function testing (){ 

        var a = Math.round( 12.34 ); 
        var b = Math[ "round" ]( 12.34 ); 

        document.getElementById( "round1" ).innerHTML = a + " , " + b; 
} 
</script>


<button onclick="example()"> 클릭하세요 </button>

<pre id="round2"></pre>

<script type="text/javascript"> function example (){ var a = Math.round( "3.4" ); var b = Math.round( "3.5" ); var c = Math.round( "3.41" ); var d = Math.round( "3.49" ); var e = Math.round( "34.41" ); var f = Math.round( "34.49" ); var str = "\n" + "a = " + a + "\n" + "b = " + b + "\n" + "c = " + c + "\n"+ "d = " + d + "\n" + "e = " + e + "\n" + "f = " + f + "\n"; document.getElementById( "round2" ).innerHTML = str; } </script>


<button onclick="example()"> 클릭하세요 </button>

<pre id="round3"></pre>

<script type="text/javascript"> function example (){ var a = Math.round( -3.4 ); var b = Math.round( -3.5 ); var c = Math.round( -3.41 ); var d = Math.round( -3.49 ); var e = Math.round( -34.41 ); var f = Math.round( -34.49 ); var str = "\n" + "a = " + a + "\n" + "b = " + b + "\n" + "c = " + c + "\n"+ "d = " + d + "\n" + "e = " + e + "\n" + "f = " + f + "\n"; document.getElementById( "round3" ).innerHTML = str; } </script>


<button onclick="example()"> 클릭하세요 </button>

<pre id="round4"></pre>

<script type="text/javascript"> function example (){ var a = Math.round( "034" ); var b = Math.round( 034 ); var c = Math.round( "0x34" ); var d = Math.round( 0x34 ); var e = Math.round( "3e4" ); var f = Math.round( 3e4 ); var str = "\n" + "a = " + a + "\n" + "b = " + b + "\n" + "c = " + c + "\n"+ "d = " + d + "\n" + "e = " + e + "\n" + "f = " + f + "\n"; document.getElementById( "round4" ).innerHTML = str; } </script>


<button onclick="example()"> 클릭하세요 </button>

<pre id="round5"></pre>

<script type="text/javascript"> function example (){ var a = Math.round( "0.50" ); var b = Math.round( 0.50 ); var c = Math.round( ".5" ); var d = Math.round( .5 ); var e = Math.round( "0.5" ); var f = Math.round( 0.5 ); var g = Math.round( "00.5" ); var str = "\n" + "a = " + a + "\n" + "b = " + b + "\n" + "c = " + c + "\n"+ "d = " + d + "\n" + "e = " + e + "\n" + "f = " + f + "\n" + "g = " + g + "\n"; document.getElementById( "round5" ).innerHTML = str; } </script>

참고로,
아래와 같이 작성한 경우에는, 오류가 나온다.
var h = Math.round( 00.5 );



<button onclick="example()"> 클릭하세요 </button>

<pre id="round6"></pre>

<script type="text/javascript"> function example (){ var a = Math.round( true ); var b = Math.round( false ); var c = Math.round( null ); var d = Math.round( undefined ); var e = Math.round( "" ); var str = "\n" + "a = " + a + "\n" + "b = " + b + "\n" + "c = " + c + "\n"+ "d = " + d + "\n" + "e = " + e + "\n"; document.getElementById( "round6" ).innerHTML = str; } </script>


이 내용이 도움이 되셨다면, 아래의 하트 버튼을 눌러주세요. *^^*