0

Example: convert calculated integer answer 24.025724 into 24.03

For the time being, I convert all decimal numbers into whole numbers to do the calculations by multiplying them by 100, then divide the answer by 100. The .toFixed method is used to remove the trailing decimals. A similar method is posted on StackOverflow: Precise Financial Calculation in JavaScript. What Are the Gotchas?

Is there a simpler way than this?

Coding language: JavaScript (vanilla) Skill level: Beginner Editor: Visual Studio Code OS: Windows 7

  • There's really no better way currently, and in any case even your approach will fail sometimes, because doing "money math" with binary floating-point numbers is not 100% reliable. The systems are not really compatible. – Pointy Apr 15 '22 at 18:09
  • Your link is a good link. I posted another one for you – mplungjan Apr 15 '22 at 18:11
  • You can store values as fractions like big decimal do – Konrad Apr 15 '22 at 20:40

0 Answers0