Operations on floating numbers in Javascript

Math in javascript is problematic. Let’s try this simple example:

    var a = 0.1;
    var b = 0.2;
    var c = a + b;
    return [{'output': c}];

Expected sum of 0.1 and 0.2 is 0.3, but js has a surprise: output : 0.30000000000000004. Such problem is common question in many forums like stackoverflow and also affects other programming languages.
More about floating-point arithmetic here:

https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

In this case, one of the simplest solution is to use method toFixed(digits). It allows to set how many digits should appear after the decimal point.
So, using formatting in the output return [{'output': c.toFixed(2)}]; will give a readable result output : 0.30

4 Likes

Primitive floating point type in JS should never be used for precise values, such as currency. toFixed method does not solve rounding error problem. In some cases toFixed() output may be more precise than toString() output. However, choosing too high precision can lead to unexpected results. For example, 0.3.toFixed(17) returns '0.29999999999999999'. Moreover, toFixed output can be rounded down if it can’t be represented exactly by a float and the closest representable float is lower. For instance, both 0.135.toFixed(2) and 0.145.toFixed(2) return '0.14'.
If you want to perform precise mathematical operations on floating point numbers in eximee, consider using the Charon service instead of Script Code. You can use the BigDecimal type for calculations.

1 Like