Recent posts

Recent comments

Archive

Calender

«   2024/03   »
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
31

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


floor의 사전적 의미.
1. 가격이나 임금 등의 최저 한도액. A base or minimum level. A lower limit.
2. 주어진 수보다 작거나 같은 가장 큰 정수. The largest integer less than or equal to a given number.
3. 해당 숫자보다 크지 않은 정수들 중에서 가장 큰 수.


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

Math.floor( 12.34 ); 

또는 

Math[ "floor" ]( 12.34 ); 


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

<pre id="floor1"> </pre>


<script type="text/javascript">

function testing (){ 

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

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


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

<pre id="floor2"> </pre>

<script type="text/javascript"> function example (){ var a = Math.floor( "3.4" ); var b = Math.floor( "3.5" ); var c = Math.floor( "3.41" ); var d = Math.floor( "3.49" ); var e = Math.floor( "34.41" ); var f = Math.floor( "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( "floor2" ).innerHTML = str; } </script>


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

<pre id="floor3"> </pre>

<script type="text/javascript"> function example (){ var a = Math.floor( -3.4 ); var b = Math.floor( -3.5 ); var c = Math.floor( -3.41 ); var d = Math.floor( -3.49 ); var e = Math.floor( -34.41 ); var f = Math.floor( -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( "floor3" ).innerHTML = str; } </script>


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

<pre id="floor4"> </pre>

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


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

<pre id="floor5"> </pre>

<script type="text/javascript"> function example (){ var a = Math.floor( "0.50" ); var b = Math.floor( 0.50 ); var c = Math.floor( ".5" ); var d = Math.floor( .5 ); var e = Math.floor( "0.5" ); var f = Math.floor( 0.5 ); var g = Math.floor( "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( "floor5" ).innerHTML = str; } </script>

참고로,
아래와 같이 작성한 경우에는, 오류가 나온다.

var h = Math.floor( 00.5 );



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