0

I need to convert a decimal value into 18 digit number. I am using Javascript Exponentiation operator for this. But in some cases, it throws wrong number. What would be the perfect solution for this scenario.

Ex:

const input = 0.00204475;
const output = input * 10 ** 18
console.log(output);

From above example, I need output like

2044750000000000

But I am getting

2044750000000000.2
Loki
  • 363
  • 4
  • 15
  • 1
    The computer stores numbers in binary, and `0.00204475` in binary has infinite decimals in which case you will get rounding errors. There's no straightforward solution to this (other than to cut off the decimals). –  Dec 20 '21 at 10:30
  • The 64-bit floating point representation, that JavaScript uses for working with numbers, cannot exactly represent 0.00204475. This is a limitation of floating point representations. Use a different representation (e.g. two integers: 204475 and the applicable exponent) – trincot Dec 20 '21 at 10:32

0 Answers0