F5F Stay Refreshed Software General Software Question Javascript Math.pow bug

Question Javascript Math.pow bug

Question Javascript Math.pow bug

D
DestinyGirl3
Junior Member
11
02-10-2023, 05:34 PM
#1
Hi there,
I was testing some JavaScript code involving Math.pow and encountered an odd problem.
Here’s the code I was using:
Code:
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))
}
}
Then I tried:
convertAmountFormat(parseFloat(a) + convertAmountFormat(1000), true)
Where a is any positive integer. For example: 2 → returns 200001000
But for a = 4 or 5 → it gives 400000999 or 500000999.
Is there a way to fix this issue?
Thanks,
salm2s
D
DestinyGirl3
02-10-2023, 05:34 PM #1

Hi there,
I was testing some JavaScript code involving Math.pow and encountered an odd problem.
Here’s the code I was using:
Code:
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))
}
}
Then I tried:
convertAmountFormat(parseFloat(a) + convertAmountFormat(1000), true)
Where a is any positive integer. For example: 2 → returns 200001000
But for a = 4 or 5 → it gives 400000999 or 500000999.
Is there a way to fix this issue?
Thanks,
salm2s

K
kcristan
Senior Member
514
02-14-2023, 11:37 AM
#2
The expectation is that the outcomes for amounts when a equals 4 or 5 should be 4.00001000 and 5.00001000 respectively.
That seems accurate.
Testing with different decimal settings—such as 7, 9, or even 10—might reveal changes.
The ".toFixed(decimals)" function alters the precision of the numbers displayed.
As for the differing return code lines in your If, Then, Else structure, it likely reflects distinct conditions or outcomes being handled.
K
kcristan
02-14-2023, 11:37 AM #2

The expectation is that the outcomes for amounts when a equals 4 or 5 should be 4.00001000 and 5.00001000 respectively.
That seems accurate.
Testing with different decimal settings—such as 7, 9, or even 10—might reveal changes.
The ".toFixed(decimals)" function alters the precision of the numbers displayed.
As for the differing return code lines in your If, Then, Else structure, it likely reflects distinct conditions or outcomes being handled.

K
koning_koala
Junior Member
40
02-15-2023, 09:09 AM
#3
Return to Computing 101, and remember how numbers—particularly floating-point values—are stored in computer memory. They use a limited number of digits, and certain experiments may not be accurately represented.
K
koning_koala
02-15-2023, 09:09 AM #3

Return to Computing 101, and remember how numbers—particularly floating-point values—are stored in computer memory. They use a limited number of digits, and certain experiments may not be accurately represented.

J
jdclay
Member
154
02-22-2023, 11:23 PM
#4
I believe I discovered a workaround:
Code:
convertAmountFormat(parseFloat(a), true) + convertAmountFormat(1000)
For this example:
Code:
convertAmountFormat(parseFloat(4), true) + convertAmountFormat(1000)
Result is 4.00001000
Appreciate your assistance
J
jdclay
02-22-2023, 11:23 PM #4

I believe I discovered a workaround:
Code:
convertAmountFormat(parseFloat(a), true) + convertAmountFormat(1000)
For this example:
Code:
convertAmountFormat(parseFloat(4), true) + convertAmountFormat(1000)
Result is 4.00001000
Appreciate your assistance

C
CfuntimeMC
Member
221
02-22-2023, 11:29 PM
#5
Necrothread. Closing.
C
CfuntimeMC
02-22-2023, 11:29 PM #5

Necrothread. Closing.