Computer >> Computer tutorials >  >> Programming >> Javascript

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


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>