Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript calculation gives too many zero digits

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:

  • Antal ansatte: 4
  • Gennemsnitlig ordreværdi kr. (ca.): 400
  • Antal ordrer årligt (ca.): 5500
  • Overskudsgrad (hvad er det?): 2.7

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

like image 340
bomortensen Avatar asked Jun 29 '11 17:06

bomortensen


People also ask

How many digits can JavaScript handle?

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.

How does one define a numeric value in JavaScript?

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.


1 Answers

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.

like image 196
Kyle d'Oliveira Avatar answered Sep 19 '22 22:09

Kyle d'Oliveira