# How to check length of a number or integer within response body

**URL:** <https://community.postman.com/t/how-to-check-length-of-a-number-or-integer-within-response-body/18724>\
**Category:** Help Hub\
**Created:** [December 28, 2020, 3:28pm UTC](https://community.postman.com/t/how-to-check-length-of-a-number-or-integer-within-response-body/18724 "2020-12-28T15:28:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martikwak](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/martikwak/32/6791_2.png) [@Martikwak](https://community.postman.com/u/Martikwak)\
**Post date:** [December 28, 2020, 3:28pm UTC](https://community.postman.com/t/how-to-check-length-of-a-number-or-integer-within-response-body/18724/1 "2020-12-28T15:28:13Z")

</div>

Hi all, I have been searching across many forums how to check in the test the length of a number but couldn’t find right answer.

For instance I got:

pm.environment.set(“IMEI”, \_.random(100000000000000,400000000000000));

or auto generated ID within response body e.g. “alternateId”: 963947565902989978,

Now, I need to make sure the number won’t be shorter or longer than 15 digits.  
Doing following won’t work a number or integer:  
pm.expect(jsonData.IMEI).to.have.lengthOf(15);

Is there any way to check the lenght of a number?

I know I can write following test, but is not what I want.  
pm.expect(jsonData.IMEI).to.eql(pm.environment.get(“IMEI”));  
pm.expect(jsonData.IMEI).to.be.above(100000000000000);  
pm.expect(jsonData.IMEI).to.be.below(400000000000000);

Thank you so much

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [December 29, 2020, 3:24am UTC](https://community.postman.com/t/how-to-check-length-of-a-number-or-integer-within-response-body/18724/2 "2020-12-29T03:24:25Z")

</div>

Hello @Martikwak,

Welcome to the Community 👏

The easiest way to have the length is to convert the numbers to String and then to toString();

```auto
var result = IMEI.toString();

pm.test("Length as expected", () => {
pm.expect(jsonData.result).to.have.lengthOf(15);
});

```

Else you can use math functions as below:

```auto
var length = Math.ceil(Math.log10(IMEI + 1));
console.log(length);
pm.test("Length as expected", () => {
pm.expect(jsonData.length).to.eql(15);
});

```

I have just worked on roughly based on what you provided. Please let me know if there are any issues and please provide me the full JSON response to work on 😉

I hope this helps 🙂

---

<div class="post-metadata">

**Author:** ![Martikwak](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/martikwak/32/6791_2.png) [@Martikwak](https://community.postman.com/u/Martikwak)\
**Post date:** [December 29, 2020, 9:32am UTC](https://community.postman.com/t/how-to-check-length-of-a-number-or-integer-within-response-body/18724/3 "2020-12-29T09:32:26Z")

</div>

Thank you so much for prompt reply @bpricilla.  
Your solution works like a charm 🙂 Very appreciate your help

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [December 29, 2020, 12:27pm UTC](https://community.postman.com/t/how-to-check-length-of-a-number-or-integer-within-response-body/18724/4 "2020-12-29T12:27:27Z")

</div>

Glad that it worked 😊

---

<div class="post-metadata">

**Author:** ![Martikwak](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/martikwak/32/6791_2.png) [@Martikwak](https://community.postman.com/u/Martikwak)\
**Post date:** [December 30, 2020, 11:48am UTC](https://community.postman.com/t/how-to-check-length-of-a-number-or-integer-within-response-body/18724/5 "2020-12-30T11:48:39Z")

</div>

Btw, using math functions (second way) the syntax instead of **.to.have.lengthOf (15)** should be **.to.eql (15)** otherwise won’t work.  
Thanks again, it was a great help and made me to study more about math functions 😊

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [December 30, 2020, 1:25pm UTC](https://community.postman.com/t/how-to-check-length-of-a-number-or-integer-within-response-body/18724/6 "2020-12-30T13:25:25Z")

</div>

Yes exactly, that’s because already you are storing the length in the variable. Sorry even I was sharing the rough scripts as I mentioned 🙂 Happy learning 🏆
