Copy link to clipboard
Copied
I've tried formating, adding in various suggest code/script but nothing is working, I've read previous posts about how the code/script worked for them but it is not working for me.
The numbers are displaying with 2 decimals but when you click on the field is shows the 14 decimal places. This throws up the simple view (of 2 decimals) when calculating.
In the example below, a user will enter the 'total sales' (field 1: $20,007.42 ), the 'total tax' is 3% of 'total sales' (field 2: $600.22 displayed/formatted for 2 decimals but when you click on the field you see it is actual the real number is: $600.2225999999999), the 'collection fee' is 3% of the 'total tax' (field 3: $18.01 displayed but real number is: $18.006677999999997), and the 'balance' is 'total tax' minus 'collection fee' (field 4: $582.22 displayed/formatted for 2 decimals, real number is: $582.215922).
When you look at it with 2 decimals (currency), the 'balance' does not add up (600.22 - 18.01 should equal 582.21 not 582.22). I understand why it is happening, I just dont know how to fix the decimal precision in the calculation to ONLY use 2 decimals.
Here is the script i have for the 'Balance' (field 4):
The other calculated fields are following the same template. I've tried several other functions (calculate strings, math.floor, round, toFixed), all not providing the outcome I want.
Can anyone help? I've already reached out to Adobe support and they told me to put in a request on their 'wish list'. I'm a programmer at my core so this doesnt make any sense and when it comes to money and what is displayed, how do you explain the real number calculation to the public who sees the 'balance' (field4) being off by one cent.
Thanks in advance.
Copy link to clipboard
Copied
If your fields are formatted as numbers, you don't need to use valueAsString then convert to a number. Just use value (eg. this.getField("Total Tax").value. If you do convert a string to a number the n in Number is supposed to be capitalized. In your sample script you declared the var1 variable twice. To answer your question, the 2-decimal formatting only formats the number. The value of the field is the actual value (with all the decimal places). If you want the actual value rounded to 2 decimal places you would use Math.round( ) like this:
event.value=Math.round(this.getField("TotalSales").value*.03*100)/100;
Then your equation will display the result you want. 100)/100 is rounding to two decimal places. For 1 decimal place, change the 100s to 10s and for 3 decimals, change the 100s to 1000s, etc.
Copy link to clipboard
Copied
If your fields are formatted as numbers, you don't need to use valueAsString then convert to a number. Just use value (eg. this.getField("Total Tax").value. If you do convert a string to a number the n in Number is supposed to be capitalized. In your sample script you declared the var1 variable twice. To answer your question, the 2-decimal formatting only formats the number. The value of the field is the actual value (with all the decimal places). If you want the actual value rounded to 2 decimal places you would use Math.round( ) like this:
event.value=Math.round(this.getField("TotalSales").value*.03*100)/100;
Then your equation will display the result you want. 100)/100 is rounding to two decimal places. For 1 decimal place, change the 100s to 10s and for 3 decimals, change the 100s to 1000s, etc.
Copy link to clipboard
Copied
Wow...ok i feel really dumb. I also noticed that the 'valueasstring' should have been 'valueAsString'. I think i was looking at this too long. Thank you for the simple solution (that was staring at me the past couple of days).
Copy link to clipboard
Copied
I edited my answer to provide a complete solution. I'm not sure if you saw it.
Copy link to clipboard
Copied
Thanks. I did see i used the var1 twice in the calculation. Again, looking at code too long. I appreciate your help (and the 2nd set of eyes on this).
Find more inspiration, events, and resources on the new Adobe Community
Explore Now