Recent posts

Recent comments

Archive

Calender

«   2024/12   »
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
truncate의 사전적 의미 
1. 끝을 자르다. 2. 꼭대기를 자르다. 3. 일부를 생략하여 줄이다. 4.끝수를 버리다. 
─ 출처 : 다음 영어사전 

[ Javascript ] Math.trunc()


Math.trunc() 함수는 소수점 이하를 전부 삭제한다.
The Math.trunc() function truncates all of the decimal places, and returns the integer part.



Polyfill

 
<script type="text/javascript">
/* 
 * source : https://tonks.tistory.com/162#_javascript_trunc
 */ 
Math.trunc = Math.trunc || function ( number ) {  return Math[ number > 0 ? "floor" : "ceil" ]( number );  } 
</script>
 

또는 (Another way is below.)


 
<script type="text/javascript">
/* 
 * source : https://tonks.tistory.com/162#_javascript_trunc
 */ 
if ( ! Math.trunc ){ 
    Math.trunc = function ( value ) { 
        var number = Number( value ); 
        return Math.abs( number ) === Infinity ? number : number - (number % 1); 
    }; 
} 
</script>
 

둘 중 어느 것이 더 실용적인지를 묻는다면,,, I don't know... (─.─ ;;)
I don't know whether any way is the more practical. (─.─ ;;)



 
<button onclick="testing1()"> Click me </button>

<p id="demo1"> </p>

<script type="text/javascript">
function testing1 (){ 

    var num = 234.56; 

    demo1.innerHTML = Math.trunc( num );
} 
</script>
 
<button onclick="testing2()"> Click me </button>

<p id="demo2"> </p>

<script type="text/javascript">
function testing2 (){ 

    var str = "234.56"; 

    demo2.innerHTML = Math.trunc( str );
} 
</script>



 
<button onclick="testing1()"> Click me </button>

<p id="demo1"> </p>

<script type="text/javascript">
function testing1 (){ 

    var num = -234.56; 

    demo1.innerHTML = Math.trunc( num );
} 
</script>
 
<button onclick="testing2()"> Click me </button>

<p id="demo2"> </p>

<script type="text/javascript">
function testing2 (){ 

    var str = "-234.56"; 

    demo2.innerHTML = Math.trunc( str );
} 
</script>



 
<button onclick="testing1()"> Click me </button>

<p id="demo1"> </p>

<script type="text/javascript">
function testing1 (){ 

    var str = "23 456"; 

    demo1.innerHTML = Math.trunc( str );
} 
</script>
 
<button onclick="testing2()"> Click me </button>

<p id="demo2"> </p>

<script type="text/javascript">
function testing2 (){ 

    var str = "23,456"; 

    demo2.innerHTML = Math.trunc( str );
} 
</script>



 
<button onclick="testing1()"> Click me </button>

<p id="demo1"> </p>

<script type="text/javascript">
function testing1 (){ 

    var str = "034"; 

    demo1.innerHTML = Math.trunc( str );
} 
</script>
 
<button onclick="testing2()"> Click me </button>

<p id="demo2"> </p>

<script type="text/javascript">
function testing2 (){ 

    var str = ""; 

    demo2.innerHTML = Math.trunc( str );
} 
</script>



 
<button onclick="testing1()"> Click me </button>

<p id="demo1"> </p>

<script type="text/javascript">
function testing1 (){ 

    var x = null; 

    demo1.innerHTML = Math.trunc( x );
} 
</script>
 
<button onclick="testing2()"> Click me </button>

<p id="demo2"> </p>

<script type="text/javascript">
function testing2 (){ 

    var x = undefined; 

    demo2.innerHTML = Math.trunc( x );
} 
</script>



 
<button onclick="testing1()"> Click me </button>

<p id="demo1"> </p>

<script type="text/javascript">
function testing1 (){ 

    var num = Infinity; 

    demo1.innerHTML = Math.trunc( num );
} 
</script>
 
<button onclick="testing2()"> Click me </button>

<p id="demo2"> </p>

<script type="text/javascript">
function testing2 (){ 

    var num = NaN; 

    demo2.innerHTML = Math.trunc( num );
} 
</script>



 
<button onclick="testing1()"> Click me </button>

<p id="demo1"> </p>

<script type="text/javascript">
function testing1 (){ 

    var boolean = true; 

    demo1.innerHTML = Math.trunc( boolean );
} 
</script>
 
<button onclick="testing2()"> Click me </button>

<p id="demo2"> </p>

<script type="text/javascript">
function testing2 (){ 

    var boolean = false; 

    demo2.innerHTML = Math.trunc( boolean );
} 
</script>





이 내용이 도움이 되셨다면, 아래의 하트 버튼을 눌러주세요. *^^*
If this article is helpful to you, please click the heart button below. *^^*