F5F Stay Refreshed Software General Software What's going on with my JavaScript math.pow bug?

What's going on with my JavaScript math.pow bug?

What's going on with my JavaScript math.pow bug?

T
The_Op_Reaper
Junior Member
19
03-08-2026, 07:04 PM
#1
Hey there! I was testing some JavaScript code with Math.pow and hit a weird problem. Here is the code I used: function convertAmountFormat(amount, invert = false) { decimals = 8 if (!invert) { return parseFloat((amount / Math.pow(10, decimals)).toFixed(decimals)) } else { return parseInt(amount * Math.pow(10, decimals)) } } After that I did: convertAmountFormat(parseFloat(a) + convertAmountFormat(1000), true) where a is any positive number. When a is, for example: 2, it returns: Code: convertAmountFormat(parseFloat(2) + convertAmountFormat(1000), true) = 200001000 But when a is either 4 or 5: Code: convertAmountFormat(parseFloat(4) + convertAmountFormat(1000), true) = 400000999 convertAmountFormat(parseFloat(5) + convertAmountFormat(1000), true) = 500000999 Is there a way I can fix this bug/issue? Thanks, salm2s
T
The_Op_Reaper
03-08-2026, 07:04 PM #1

Hey there! I was testing some JavaScript code with Math.pow and hit a weird problem. Here is the code I used: function convertAmountFormat(amount, invert = false) { decimals = 8 if (!invert) { return parseFloat((amount / Math.pow(10, decimals)).toFixed(decimals)) } else { return parseInt(amount * Math.pow(10, decimals)) } } After that I did: convertAmountFormat(parseFloat(a) + convertAmountFormat(1000), true) where a is any positive number. When a is, for example: 2, it returns: Code: convertAmountFormat(parseFloat(2) + convertAmountFormat(1000), true) = 200001000 But when a is either 4 or 5: Code: convertAmountFormat(parseFloat(4) + convertAmountFormat(1000), true) = 400000999 convertAmountFormat(parseFloat(5) + convertAmountFormat(1000), true) = 500000999 Is there a way I can fix this bug/issue? Thanks, salm2s

3
3Geschenk3
Junior Member
43
03-24-2026, 09:24 PM
#2
So do you expect these numbers to be exactly 4.00001 and 5.00001 when you plug in values of 4 or 5 into that amount? That is correct. Let's test it more: what if we change the number of decimals to 7, 9, or even 10? What does that .toFixed(decimals) do? And why are the lines inside your If, Then, Else logic different from each other?
3
3Geschenk3
03-24-2026, 09:24 PM #2

So do you expect these numbers to be exactly 4.00001 and 5.00001 when you plug in values of 4 or 5 into that amount? That is correct. Let's test it more: what if we change the number of decimals to 7, 9, or even 10? What does that .toFixed(decimals) do? And why are the lines inside your If, Then, Else logic different from each other?

T
taconiebre
Senior Member
506
03-31-2026, 12:21 PM
#3
Back in Computing 101, remember how numbers work when they go into a computer's memory. Computers can only hold so many digits at once, which is why some tests sometimes don't work exactly right because things aren't perfect.
T
taconiebre
03-31-2026, 12:21 PM #3

Back in Computing 101, remember how numbers work when they go into a computer's memory. Computers can only hold so many digits at once, which is why some tests sometimes don't work exactly right because things aren't perfect.