1

I assumed if the first number after the decimal is 5 or higher, it would rounds up

Here I'm calculating 5% of 31.5.

Problem #1

It shouldn't matter which operation happens first but it does

console.log((31.5 * 5)/100) // 1.575

console.log(31.5 * (5/100)) // 1.5750000000000002

Problem #2

When I round it to 2 decimal point I don't get the same

console.log(((31.5 * 5)/100).toFixed(2)) // 1.57

console.log((31.5 * (5/100)).toFixed(2)) // 1.58

Why 1.575 is not rounding up to 1.58?

vrintle
  • 5,501
  • 2
  • 16
  • 46
pref
  • 1,651
  • 14
  • 24
  • Does this answer your question? [Rounding up and rounding down bill amount](https://stackoverflow.com/questions/8352834/rounding-up-and-rounding-down-bill-amount) – malarres Aug 06 '20 at 06:24
  • thanks for the link. so it appears I shouldn't use "toFixed()" and instead use "Math.round()" I'm assuming there is a bug in toFixed e.g. let x = 1.575; Math.round( x * 100)/100 ----> 1.58 but x.toFixed(2) ----> 1.57 toFixed would round up to 1.58 if x was 1.5750000000001 – pref Aug 10 '20 at 17:48

0 Answers0