# Validate a number to have 2 decimal points

**URL:** <https://community.postman.com/t/validate-a-number-to-have-2-decimal-points/42803>\
**Category:** Help Hub\
**Created:** [January 11, 2023, 6:37pm UTC](https://community.postman.com/t/validate-a-number-to-have-2-decimal-points/42803 "2023-01-11T18:37:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![u-hussain](https://avatars.discourse-cdn.com/v4/letter/u/f07891/32.png) [@u-hussain](https://community.postman.com/u/u-hussain)\
**Post date:** [January 11, 2023, 6:37pm UTC](https://community.postman.com/t/validate-a-number-to-have-2-decimal-points/42803/1 "2023-01-11T18:37:03Z")

</div>

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 ();

pm.test(“Price has 2 decimal points”, function (){  
pm.expect(jsondata.Details.rates[0].price).toFixed( 2 )

});

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [January 12, 2023, 12:17pm UTC](https://community.postman.com/t/validate-a-number-to-have-2-decimal-points/42803/2 "2023-01-12T12:17:44Z")

</div>

Your going to need a function to count the decimal places.

Something like.

```auto
// https://stackoverflow.com/questions/27082377/get-number-of-decimal-places-with-javascript

var countDecimals = function(value) {
  let text = value.toString()
  // verify if number 0.000005 is represented as "5e-6"
  if (text.indexOf('e-') > -1) {
    let [base, trail] = text.split('e-');
    let deg = parseInt(trail, 10);
    return deg;
  }
  // count decimals for number in representation like "0.123456"
  if (Math.floor(value) !== value) {
    return value.toString().split(".")[1].length || 0;
  }
  return 0;
}

let value1 = 37.435345;
let decimalPlaces1 = countDecimals(value1) //6

let value2 = 20.00; 
// note the 00 will be stripped into JavaScript, so the assertion is looking for less than 3 instead of eql 2.
let decimalPlaces2 = countDecimals(value2) //0

let value3 = 9.90; 
// the zero will also get stripped
let decimalPlaces3 = countDecimals(value3) //1

let value4 = "9.90"; 
// the function can take strings, so you could stringify the number when retrieving it.
let decimalPlaces4 = countDecimals(value4) // 2

let value5 = "9.901"; 
// boundary test
let decimalPlaces5 = countDecimals(value5) // 3

pm.test(`1: Two decimal places : ${value1}`, () => {
    pm.expect(decimalPlaces1).to.be.lessThan(3);
});

pm.test(`2: Two decimal places : ${value2}`, () => {
    pm.expect(decimalPlaces2).to.be.lessThan(3);
});

pm.test(`3: Two decimal places : ${value3}`, () => {
    pm.expect(decimalPlaces3).to.be.lessThan(3);
});

pm.test(`4: Two decimal places : ${value4}`, () => {
    pm.expect(decimalPlaces4).to.be.lessThan(3);
});

pm.test(`5: Two decimal places : ${value5}`, () => {
    pm.expect(decimalPlaces5).to.be.lessThan(3);
});

```

![image](https://us1.discourse-cdn.com/getpostman/original/3X/0/e/0ee3cec61fe021a38f8008867914db0344bffddd.png)

---

<div class="post-metadata">

**Author:** ![u-hussain](https://avatars.discourse-cdn.com/v4/letter/u/f07891/32.png) [@u-hussain](https://community.postman.com/u/u-hussain)\
**Post date:** [January 12, 2023, 2:22pm UTC](https://community.postman.com/t/validate-a-number-to-have-2-decimal-points/42803/3 "2023-01-12T14:22:12Z")

</div>

Hey @michaelderekjones, This is absolutely brilliant.

This work, thank you for the help.

---

<div class="post-metadata">

**Author:** ![alexisnavarro24](https://avatars.discourse-cdn.com/v4/letter/a/c77e96/32.png) [@alexisnavarro24](https://community.postman.com/u/alexisnavarro24)\
**Post date:** [February 24, 2023, 1:10pm UTC](https://community.postman.com/t/validate-a-number-to-have-2-decimal-points/42803/4 "2023-02-24T13:10:23Z")

</div>

Alternatively, if you really just need to check for **2 decimals or less** specifically, you can also do this:

```auto
const hasAtMostTwoDecimals = (number) => number * 100 % 1 === 0

const price = jsondata.Details.rates[0].price
pm.expect(hasAtMostTwoDecimals(price)).to.be.true

```
