How do I convert a float number to a whole number in JavaScript?

JavascriptWeb DevelopmentFront End Technology

Use truncating and rounding in JavaScript to convert a float number to a whole number.

Example

You can try to run the following code to convert float to the whole number:

<!DOCTYPE html>
<html>
   <body>
   <p>Values:</p>
      <script>
         var myFloat = 1.5;
         var myTrunc = Math.trunc( myFloat );
         var myCeil = Math.ceil( myFloat );
         var myFloor = Math.floor( myFloat );
         var myRound = Math.round( myFloat );
         document.write(myTrunc+ " ");
         document.write(myCeil+ " ");
         document.write(myFloor+ " ");
         document.write(myRound+ " ");
      </script>
   </body>
</html>
raja
Published on 09-Jan-2018 09:23:20
Advertisements