How to use JavaScript to check if a number has a decimal place or it’s a whole number?

JavascriptWeb DevelopmentFront End Technology

Use the Math.floor method to check whether a number has a decimal place or not.

Example

You can try to run the following code to check if a number has a decimal place or not −

Live Demo

<html>
   <head>
      <title>JavaScript Numbers</title>
   </head>
   
   <body>
      <script>
         var num = 10.2;
         if (num == Math.floor(num)) {
            alert("Whole Number")
         } else {
            alert("Decimal Number")
         }
      </script>
   </body>
</html>
raja
Published on 18-Jan-2018 11:53:23
Advertisements