JavaScript assertion results in scientific notation
search cancel

JavaScript assertion results in scientific notation

book

Article ID: 191079

calendar_today

Updated On: 11-20-2024

Products

CA API Gateway

Issue/Introduction

When working with certain floating-point numbers the JavaScript assertion produces a result in scientific notation when a decimal is expected.

Environment

All supported versions of the CA API Gateway

Resolution

Numbers are treated as a BigDecimal as opposed to a double in our assertion which causes results in scientific notation.
If a decimal is required the toString() method can be called to retain a certain level of precision.

For Example:
Given the expression 163066.42 * 100 will result in 1.6306642000000002E7
context.setVariable("receiveAmountDecimal", receiveAmountDecimal.toString());
After executing toString() will produce this decimal value.

16306642.000000002

If precision is not an issue and you wish to disregard the fractional part or return only a fixed number of digits in the fractional part you can use

context.setVariable("receiveAmountDecimal", receiveAmountDecimal.toFixed(x));

Where will be the number of digits to retain to the right of the decimal. If no precision is needed simply set this to 0.