The operator "*" and "/" do not work perfectly always, for example:
32.09 * 100 = 3209.0000000000005
100 / (1 / 32.09) = 3209.0000000000005
Solution:
One solution, the easy one, to have decimals is useing .toFixed(2) where "2" is the number of decimal digits that you want. But you have to take into account this return you a String, and it rounds the value.
45.6789.toFixed(2) —> "45.68" as String
Other option, the most complete, is using .toLocaleString that converts the number properly to the country code that you want. You can set a pair of params to fix the decimals values always to 2. This also returns you a String:
(654.3453).toLocaleString(
'en-US',
{minimumFractionDigits: 2, maximumFractionDigits: 2},
) —> "654.35" as String
If you need it as a Number, you can wrap it into Number( ... ) :
Number((654.3453).toLocaleString(
'en-US',
{minimumFractionDigits: 2, maximumFractionDigits: 2},
)) —> 654.35 as Number
If you only want a no decimal number, use one of this methods to be sure that the result is without decimals.
Math.trunc(32.09); —> 32 as Number
(32.09).toFixed(0); —> “32” as String
32.09 | 0; —> 32 as Number