I think it's time get some second opinions on the javascript I'm working on at the moment.
I'm trying to create a calculator to tell people if they can save money by using a subscription etc etc. The calculation itself is done by som inputs from the client and some prevalues from Umbraco CMS.
The result I get from the calculator at the moment is "right" in that the actual numbers are correct, but there's just too many zeroes in it.
The calculator can be found here: my calculator
The test data I'm using is the following:
Which gives the output: 712800.0000000001
I tried to divide this by 100 and, of course, it just moved the comma/dot two steps the left. Still there's all those zeroes.
Any help/hint is greatly appreciated! :-)
Thanks in advance,
Bo
P.S. The result I'm looking for would be 7128.00
You can think that in Javascript, integer numbers are accurate up to 15 digits. In this post, I'll present two different strategies to overcome these precision issues: one for decimal numbers and another for integer numbers.
Number is a primitive wrapper object used to represent and manipulate numbers like 37 or -9.25 . The Number constructor contains constants and methods for working with numbers. Values of other types can be converted to numbers using the Number() function.
I believe you want to call toFixed on your integer.
(712800.00000001/100).toFixed(2)
"7128.00"
It will change your number into a string, but if it is for display purposes it should be fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With