StoryCode

실수를 정수로 바꾸는 방법

JavaScript, ECMAScript
반응형

참조 : https://www.geeksforgeeks.org/how-to-convert-a-float-number-to-the-whole-number-in-javascript/

 

 

 

How to convert a float number to the whole number in JavaScript?

There are various methods to convert float number to the whole number in JavaScript.

  1. Math.floor (floating argument): Round off the number passed as parameter to its nearest integer in Downward direction.
    Syntax:Math.floor(value)

    filter_none

    edit

    play_arrow

    brightness_4

    <script>

      //float value is 4.59;

       var x = 4.59;

       var z = Math.floor(x);

       document.write("Converted value of " + x + " is " + z);

    </script>

    Output :

    Converted value of 4.59 is 4
  2. Math.ceil (floating argument): Return the smallest integer greater than or equal to a given number.
    Syntax:Math.ceil(value)

    filter_none

    edit

    play_arrow

    brightness_4

    <script>

      //float value is 4.59;

       var x = 4.59;

       var z = Math.ceil(x);

       document.write("Converted value of " + x + " is " + z);

    </script>

    Output :

    Converted value of 4.59 is 5
  3. Math.round (floating argument): Round a number to its nearest integer.
    Syntax:Math.round(var);

    filter_none

    edit

    play_arrow

    brightness_4

    <script>

      //float value is 4.59;

       var x = 4.59;

       var z = Math.round(x);

       document.write("Converted value of " + x + " is " + z);

    </script>

    Output :

    Converted value of 4.59 is 5
  4. Math.trunc (floating argument): Return the integer part of a floating-point number by removing the fractional digits.
    Syntax:Math.trunc(value)

    filter_none

    edit

    play_arrow

    brightness_4

    <script>

      //float value is 4.59;

       var x = 4.59;

       var z = Math.trunc(x);

       document.write("Converted value of " + x + " is " + z);

    </script>

    Output :

    Converted value of 4.59 is 4
  5. parseInt (floating argument): Accept the string and convert it into an integer.
    Syntax:parseInt(Value, radix)

    filter_none

    edit

    play_arrow

    brightness_4

    <script>

      //float value is 3.54;

       var x = 3.54;

       var z = parseInt(x);

       document.write("Converted value of " + x + " is " + z);

    </script>

    Output :

    Converted value of 3.54 is 3
  6. double bitwise not (~~) operator: Round a number to towards zero. If the operand is a number and it’s not NaN or Infinity.
    Syntax:~~value

    filter_none

    edit

    play_arrow

    brightness_4

    <script>

      //float value is 4.59;

       var x = 4.59;

       var z = ~~x;

       document.write("Converted value of " + x + " is " + z);

    </script>

    Output :


    Converted value of 4.59 is 4
  7. bitwise OR (|) operator: Round a number to towards zero.
    Syntax:var = value | 0;

    filter_none

    edit

    play_arrow

    brightness_4

    <script>

      //float value is 5.67;

       var x = 5.67;

       var z = x | 0;

       document.write("Converted value of " + x + " is " + z);

    </script>

    Output :

    Converted value of 5.67 is 5
  8. Using shift (>>) operator: Round a number to towards zero.
    Syntax:var = value >> 0;

    filter_none

    edit

    play_arrow

    brightness_4

    <script>

      //float value is 5.63;

       var x = 5.63;

       var z = x >> 0; 

       //it is same as we are dividing the value by 1.

       document.write("Converted value of " + x + " is " + z);

    </script>

    Output :

    Converted value of 5.63 is 5
  9. Using unsigned shift (>>>) operator Round a number to towards zero.
    Syntax:var = value >>> 0;

    filter_none

    edit

    play_arrow

    brightness_4

    <script>

      //float value is 5.68;

       var x = 5.68;

       //it is same as we are dividing the value by 1.

       var z = x >>> 0; 

       document.write("Converted value of " + x + " is " + z);

    </script>

    Output :

    Converted value of 5.68 is 5
  10. By subtracting the fractional part
    Syntax:var = val - val%1;

    filter_none

    edit

    play_arrow

    brightness_4

    <script>

      //float value is 5.48;

       var x = 5.48;

       var z = x - x%1;

       document.write("Converted value of " + x + " is " + z);

    </script>

    Output :

    Converted value of 5.48 is 5
  11. Using XOR (^) operator
    Syntax:var = value ^ 0;

    filter_none

    edit

    play_arrow

    brightness_4

    <script>

      //float value is 5.49;

       var x = 5.49;

       var z = x ^ 0;

       document.write("Converted value of " + x + " is " + z);

    </script>

    Output :

    Converted value of 5.49 is 5

 

반응형

'JavaScript, ECMAScript' 카테고리의 다른 글

자바스크립트 역사.  (0) 2020.09.21
Count, 카운트  (0) 2020.03.09
Hoisting.호이스팅  (0) 2020.02.12
자바스크립트.변수.모니터링워처  (0) 2020.02.01
한글 받침 구별법  (0) 2019.04.15