Parse the number into a string, split by a period and check whether the second item's length is 2:

function isPrecise(num){
  return String(num).split(".")[1]?.length == 2;
}

console.log(isPrecise(1.23))
console.log(isPrecise(1.2))
console.log(isPrecise(1))
console.log(isPrecise("1.23")) //works if its a string too

You can also use a regex:

function isPrecise(num){
  return /\d+\.\d{2}/gm.test(String(num));
}

console.log(isPrecise(1.23))
console.log(isPrecise(1.2))
console.log(isPrecise("1.23")) //works if its a string too

Answer from Spectric on Stack Overflow
🌐
Futurestud.io
futurestud.io › tutorials › check-if-a-number-has-decimal-places-in-javascript-or-node-js
Check If a Number has Decimal Places in JavaScript or Node.js
May 4, 2023 - /** * Determine whether the given `num` has decimal places. * * @param {Number} num * * @returns {Boolean} */ function hasDecimalPlaces (num) { return !Number.isSafeInteger(num) } Please notice: JavaScript comes with precision limits on floating-point numbers.
Discussions

Validate a number to have 2 decimal points
Hey All, Wanted to know if we can create test to validate a certain number in response that has only 2 decimal place, if it has more than 2 decimal places then its a fail test. e.g “Details”: { “rates”: [ { “price”: 9.90 } e.g something like var jsondata = pm.response.json (); ... More on community.postman.com
🌐 community.postman.com
0
0
January 12, 2023
javascript - How to check if a number has two decimal places and keep the trailing zero if necessary? - Stack Overflow
If you have these numbers: 2 2.05 2.547 2.5 How can I do a check to see if the number has two decimal places? If it has 2 decimals, how do you drop the second decimal number (rounding) but keep the More on stackoverflow.com
🌐 stackoverflow.com
jquery - Simplest way of getting the number of decimals in a number in JavaScript - Stack Overflow
As JavaScript is a size critical language, you could omit the else statement. ie.. if(true) return x; return 0; ... basically if it does not return the amount of decimals, just return 0; 2013-06-28T16:34:58.98Z+00:00 ... yeap it would be faster using ternary but I think it's easier to read and understand the concept. OP can change that concept to a more ... More on stackoverflow.com
🌐 stackoverflow.com
javascript - Check if a number has a decimal place/is a whole number - Stack Overflow
I am looking for an easy way in JavaScript to check if a number has a decimal place in it (in order to determine if it is an integer). For instance, 23 -> OK 5 -> OK 3.5 -> not OK 34.345... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Reddit
reddit.com › r/learnjavascript › how to check if two decimal results are equal?
r/learnjavascript on Reddit: How to check if two decimal results are equal?
November 24, 2024 -
let a = 0;
a += 0.8;
a += 0.4;
// a = 1.2000000000000002

let b = 0;
b += 0.6;
b += 0.6;
// b = 1.2

How can I check the results are the same if a === b is false?

Top answer
1 of 9
7
Usually this is done by subtracting the values and seeing if the delta is within a certain threshold (epsilon). Math.abs(a - b) <= 1e-7 // true - values are close enough to be considered equal The link chmod777 posted links to some good resources if you want to go deep into the matter, and as PyroGreg8 it really does help if you can stick to integers where possible. A little more on the issue with respect to this specific example: The Number format can only represent so many numbers, and because it can represent such a wide range of numbers there are gaps between each number it can represent. Unfortunately, these gaps don't always align on certain decimal values meaning many decimal values aren't exactly what they appear to be, rather they're approximations. The value 1.2 is an example of a number that can't be represented exactly. Its approximated using a value that's very close to 1.2. Using toFixed() you can see a more specific value for what is used when you type and use the literal 1.2: console.log(1.2.toFixed(30)) // 1.199999999999999955591079014994 You can see the actual value for 1.2 is slightly smaller, but still extremely close. Most of the time, these differences are so small you never notice them. However, sometimes when performing mathematical operations on these numbers, rounding/precision errors can put you on one side or the other of the real value. For example 0.8 + 0.4 rounds up while 0.6 + 0.6 rounds down. console.log((0.8 + 0.4).toFixed(30)) // 1.200000000000000177635683940025 console.log((0.6 + 0.6).toFixed(30)) // 1.199999999999999955591079014994 Both of these values are basically both the value "1.2", or the closest it can get to 1.2 on either side of the actual value. The gap between them is around 2.220446e-16. The Number format simply can't represent any other values in between. It just so happens that the literal values for both 0.8 and 0.4 (also approximations) use a higher value for their representations. console.log(0.8.toFixed(30)) // 0.800000000000000044408920985006 console.log(0.4.toFixed(30)) // 0.400000000000000022204460492503 So in combining them together, the result is the value of 1.2 closest to it from above. As you've probably already guessed, 0.6 uses a smaller value console.log(0.6.toFixed(30)) // 0.599999999999999977795539507497 Which makes sense why adding 0.6 and 0.6 puts you on the lower side of 1.2. The unfortunate result of this is that while these to operations should result in the same value, because each value is already being approximated, the approximated results just happen to not align. When it comes to comparisons, we similarly need to take the approach of approximate equality rather than exact equality.
2 of 9
4
https://0.30000000000000004.com/
🌐
Postman
community.postman.com › help hub
Validate a number to have 2 decimal points - Help Hub - Postman Community
January 12, 2023 - Hey All, Wanted to know if we can ... var jsondata = pm.response.json (); pm.test(“Price has 2 decimal points”, function (){ pm.expect(jsondata.Details.rates[0].price).toFixed( 2 ) });...
🌐
W3Schools
w3schools.com › jsref › jsref_tofixed.asp
JavaScript toFixed() Method
If the number of decimals are higher than in the number, zeros are added.
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-use-javascript-to-check-if-a-number-has-a-decimal-place-or-it-s-a-whole-number
How to use JavaScript to check if a number has a decimal place or it's a whole number?
2 weeks ago - <!DOCTYPE html> <html> <body> <h3>Check ... <button onclick="checkWithIsInteger()">Check Number</button> <script> function checkWithIsInteger() { var input = document.getElementById("input2"); var result = document.getEleme...
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-validate-decimal-numbers-in-javascript
How to Validate Decimal Numbers in JavaScript ? - GeeksforGeeks
July 23, 2025 - function validateDecimalNumberUsingRegExp(input) { // Define a regular expression for decimal numbers // This pattern allows for an optional negative sign, followed by digits, an optional decimal point, and more digits const decimalPattern = /^-?\d*\.?\d+$/; // Test if the input matches the pattern return decimalPattern.test(input); } // Example usage: const userInput = "3.14"; if (validateDecimalNumberUsingRegExp(userInput)) { console.log("Approach 2: Valid decimal number!"); } else { console.log("Approach 2: Not a valid decimal number."); } Output · Approach 2: Valid decimal number! Comment · Article Tags: Article Tags: JavaScript ·
Top answer
1 of 3
158
Number.prototype.countDecimals = function () {
    if(Math.floor(this.valueOf()) === this.valueOf()) return 0;
    return this.toString().split(".")[1].length || 0; 
}

When bound to the prototype, this allows you to get the decimal count (countDecimals();) directly from a number variable.

E.G.

var x = 23.453453453;
x.countDecimals(); // 9

It works by converting the number to a string, splitting at the . and returning the last part of the array, or 0 if the last part of the array is undefined (which will occur if there was no decimal point).

If you do not want to bind this to the prototype, you can just use this:

var countDecimals = function (value) {
    if(Math.floor(value) === value) return 0;
    return value.toString().split(".")[1].length || 0; 
}

EDIT by Black:

I have fixed the method, to also make it work with smaller numbers like 0.000000001

Number.prototype.countDecimals = function () {

    if (Math.floor(this.valueOf()) === this.valueOf()) return 0;

    var str = this.toString();
    if (str.indexOf(".") !== -1 && str.indexOf("-") !== -1) {
        return str.split("-")[1] || 0;
    } else if (str.indexOf(".") !== -1) {
        return str.split(".")[1].length || 0;
    }
    return str.split("-")[1] || 0;
}


var x = 23.453453453;
console.log(x.countDecimals()); // 9

var x = 0.0000000001;
console.log(x.countDecimals()); // 10

var x = 0.000000000000270;
console.log(x.countDecimals()); // 13

var x = 101;  // Integer number
console.log(x.countDecimals()); // 0

2 of 3
52

Adding to series0ne answer if you want to have the code not throw an error for an integer number and get a result of 0 when there are no decimals use this:

var countDecimals = function (value) { 
    if ((value % 1) != 0) 
        return value.toString().split(".")[1].length;  
    return 0;
};
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-parse-float-with-two-decimal-places-in-javascript
How to Parse Float with Two Decimal Places in JavaScript? | GeeksforGeeks
The parseFloat() method converts a string to a floating-point number. If the string isn't numeric, it returns NaN. To limit the number to two decimal places, use toFixed(2), which rounds the result.
Published   January 9, 2025
🌐
IQCode
iqcode.com › code › javascript › how-to-know-if-a-number-has-a-decimal-number-js
how to know if a number has a decimal number js Code Example
September 26, 2021 - This method returns true if the value is of the type Number, and an integer (a number without decimals). Otherwise it returns false. ... Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond. Sign up · Develop soft skills on BrainApps Complete the IQ Test ... check number is whole no decimal javascript check is a number decimal js js if the number has decimal how to check decimal number in javascript how to check if a variable is a decimal in js how to find out if a number is decimal in javascript check if decimal or integer javascript how to check if an nu
🌐
SitePoint
sitepoint.com › javascript
1 Decimal Validation - JavaScript - SitePoint Forums | Web Development & Design Community
November 10, 2011 - Dear All, I can check now a field is isNumeric via this method var isNumeric = /[1]+$/;. My problem now user can put two or more decimal and I want to limit just to single decimal and only 2 numbers before the decimal? …
🌐
TutorialsPoint
tutorialspoint.com › how-to-use-javascript-to-check-if-a-number-has-a-decimal-place-or-it-s-a-whole-number
How to use JavaScript to check if a number has a decimal place or it’s a whole number?
November 25, 2022 - In the above example, we have used the isInteger() method to check whether a number is a whole number or it is a decimal number, and we can see that the code is working. In JavaScript, we can define our own method to check whether a number input by the user is a decimal number or a whole number.
🌐
freeCodeCamp
forum.freecodecamp.org › t › javascript-calculator-how-to-avoid-two-decimals-in-a-number › 350394
Javascript Calculator : How to avoid two decimals in a number? - The freeCodeCamp Forum
February 15, 2020 - There needs some help with test case 11 & 13 which needs avoidment of multiple decimals in a number and 13th test case where continuous inputs of multiple operators between two numbers the last operator is to be evaluated with one case of negative sign possibility 5 * -5. Javascript Calculator
🌐
Castor
helpdesk.castoredc.com › general-calculation-templates › check-if-a-number-contains-x-number-of-digits-or-decimal-places
Check if a number contains X number of digits or decimal places in EDC/CDMS – Castor
Using calculation fields, it is possible to check if the number that is entered in a numerical field corresponds to a certain format. For example, you can check if a number contains 3 digits or if it contains 2 decimals. This template will check how many digits a number contains: ... Set up a data validation in case the number exceeds or is less than the necessary number of digits.