-1

So I am having issues with numbers:

I have a item, with a price of 483.65. I want to multiply this by 7. Expected result should be:

3385.55

However, Javascript reds this as:

3385.5499999999997

The number in the database is stored as a Float.

Here are screenshots to explain the situation better:

enter image description here

What am I missing?

uksz
  • 18,239
  • 30
  • 94
  • 161

1 Answers1

1

be careful with floating point math operations. thats because of floating point standard used by js vm. one trick I found to fix it without external libs is to multiple by 1k and than divid by 1k. 483.65 * 1000 * 7 / 1000 strange but always works.

sagie
  • 1,744
  • 14
  • 15
  • And order would matter: `483.65*1000*7/1000 = 3385.55`, whereas `483.65*7*1000/1000 = 3385.5499999999997`. – Uzbekjon Aug 31 '16 at 11:32
  • of course. need to use the same order as i wrote in my comment. – sagie Aug 31 '16 at 11:33
  • This might work by coincidence, but you should not rely on that (read the IEEE 754 standard). Use `toFixed()`, integer arithmetic or an arbitrary precision library, depending on your requirements. – Tobias Aug 31 '16 at 12:18