1

The Situation I am running into is that I am doing a basic test and it appears the response.json() is truncating trailing zeros. As this is a money field, I would like to validate the response is sending those zero. Here is my example:

Original Response:

    .
    .
    "amount": 100.00,
    .
    .

After running the conversion:

    const responseJson = pm.response.json();

the line reads:

    .
    .
    "amount":100,
    .
    .

I am no longer able to test if this originally had the two decimal places.

For what it is worth, I started down the path of converting to string, but it is already truncated by the time it get to the pm.expect statement so I am not able to regex it.

Any solutions other than trying to get originator of the api to return a string instead of a number (I don't like that for many reasons).

Eric VanRoy
  • 11
  • 1
  • 2

1 Answers1

1

Classic JavaScript. Trying to force you to spin up a personalized workaround for something seemingly simple :)

EDIT: Assuming you don't need to worry about rounding...

var amount = pm.response.json().yourPathTo.amount.toFixed(2);

console.log(amount); // "100.00"

Note that object type is a string, due to the return type of the toFixed function.

var response = {
    "product": "juice",
    "amount": 100.00,
    "stock?": true
};

console.log(typeof response.amount.toFixed(2), response.amount.toFixed(2)); // "string" "100.00"

console.log(typeof parseInt(response.amount.toFixed(2)), response.amount.toFixed(2)); // "number" "100.00"

console.log(typeof parseFloat(response.amount.toFixed(2)), response.amount.toFixed(2)); // "number" "100.00"

Returning amount as a string in the response body is probably the route I (personally) would choose. But, I believe this is what you are looking for.

If you need to consider rounding, use this:

function roundToTwo(num)
{
    return +(Math.round(num + "e+2")  + "e-2");
}

Thanks to MarkG for his response to this question

Stroh
  • 35
  • 5