How to Check Whether a Number is Decimal or Not in Javascript
In this tutorial, you will learn how to check whether a number is decimal or not in javascript. A number whose whole number part and fraction part is separated by decimal point is known as a decimal number. We have float
primitive type to handle decimal numbers in javascript.
There are numerous ways to detect if a number is a decimal number or not, but I am going to share one of the simplest methods. This method involves the usage of the parseFloat()
method and modulus operator (%
).
The parseFloat()
method will take a string as a parameter and return a floating-point number. It may or may not have decimal because it depends if a string contains a whole number or decimal number. The modulus operator (%
) returns the remainder of a division.
In the following example, we are going to enter a decimal number in the input field and use a combination of parseFloat()
method and modulus operator (%
) to detect whether the number is a decimal or not. Depending upon the result, we will display a Boolean value on the screen. Please have a look over the code example and steps given below.
HTML & CSS
- We have 4 elements in the HTML file and that includes
div
,input
,button
, andh1
elements. - The
div
element is just a wrapper for the rest of the elements. - The inner text for the
button
element is“Get”
and for theh1
button, it is“Result”
. - We have done some basic styling using CSS and added the link to our
style.css
stylesheet inside thehead
element. - We have also included our javascript file
script.js
with ascript
tag at the bottom.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <link rel="stylesheet" href="style.css"> <title>Document</title> </head> <body> <div> <input type="number"> <button>Get</button> <h1>Result</h1> </div> <script src="script.js"></script> </body> </html>
div { text-align: center; } input, button { display: inline-block; padding: 10px 20px; }
Javascript
- We have selected
button
,input
, andh1
elements usingdocument.querySelector()
and stored them inbtnGet
,input
, andresult
variables respectively. - We have attached the
click
event listener to thebutton
element. - In the event handler function, we are using the
parseFloat()
method and passing the value of theinput
element to convert the string to a decimal number. We are storing the returned value in thenum
variable. - We have to divide
num
by 1 and get the remainder. To achieve that, we are using the modulus operator (%
). If the remainder is 0, that means the number is a whole number. Otherwise, it is a decimal number. This calculation results in a Boolean value. - We are displaying that Boolean value in the
h1
element.
let btnGet = document.querySelector('button'); let input = document.querySelector('input'); let result = document.querySelector('h1'); btnGet.addEventListener('click', () => { let num = parseFloat(input.value); result.innerText = num % 1 != 0 ? 'True' : 'False'; });