Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parseFloat rounding

I have javascript function that automatically adds input fields together, but adding numbers like 1.35 + 1.35 + 1.35 gives me an output of 4.050000000000001, just as an example. How can I round the total to the second decimal instead of that long string?

The input fields will have more than just the 1.35 example so I need the total to never have more than 2 points after the decimal. Here is the full working code:

<html> <head> <script type="text/javascript"> function Calc(className){ var elements = document.getElementsByClassName(className); var total = 0;  for(var i = 0; i < elements.length; ++i){ total += parseFloat(elements[i].value); }  document.form0.total.value = total; }  function addone(field) {   field.value = Number(field.value) + 1;   Calc('add'); } </script> </head> <body> <form name="form0" id="form0"> 1: <input type="text" name="box1" id="box1" class="add" value="0" onKeyUp="Calc('add')" onChange="updatesum()" onClick="this.focus();this.select();" /> <input type="button" value=" + " onclick="addone(box1);"> <br />  2: <input type="text" name="box2" id="box2" class="add" value="0" onKeyUp="Calc('add')" onClick="this.focus();this.select();" /> <input type="button" value=" + " onclick="addone(box2);"> <br />  <br /> Total: <input readonly style="border:0px; font-size:14; color:red;" id="total" name="total"> <br /> </form> </body></html> 

Some things I have tried, which should work but I am clearly implementing them incorrectly:

for(var i = 0; i < elements.length; ++i){ total += parseFloat(elements[i].value.toString().match(/^\d+(?:\.\d{0,2})?/));  var str = total.toFixed(2); 

or

for(var i = 0; i < elements.length; ++i){ total += parseFloat(elements[i].value * 100) / 100).toFixed(2) 

Have also had no luck with Math.floor

like image 457
JB. Avatar asked Jan 09 '11 16:01

JB.


People also ask

Does parseFloat round up?

Return value: It returns the number or string rounded up to the specified places after the decimal. If the specified value is greater than the number of digits after decimal in the actual string then the resulting value is padded with 0 to maintain the number of digits after decimal in the final output.

What does parseFloat () do?

parseFloat() The parseFloat() function parses an argument (converting it to a string first if needed) and returns a floating point number.

Is parseFloat a method?

The parseFloat() method in Float Class is a built in method in Java that returns a new float initialized to the value represented by the specified String, as done by the valueOf method of class Float. Parameters: It accepts a single mandatory parameter s which specifies the string to be parsed.

Does parseInt round down?

parseInt() is an easy way to parse a string value and return a rounded integer. If you're working with a special use-case and require a different numerical base, modify the radix to your choosing.


1 Answers

Use toFixed() to round num to 2 decimal digits using the traditional rounding method. It will round 4.050000000000001 to 4.05.

num.toFixed(2); 

You might prefer using toPrecision(), which will strip any resulting trailing zeros.

Example:

1.35+1.35+1.35 => 4.050000000000001 (1.35+1.35+1.35).toFixed(2)     => 4.05 (1.35+1.35+1.35).toPrecision(3) => 4.05  // or... (1.35+1.35+1.35).toFixed(4)     => 4.0500 (1.35+1.35+1.35).toPrecision(4) => 4.05 

Reference: JavaScript Number Format - Decimal Precision

like image 116
moinudin Avatar answered Sep 21 '22 22:09

moinudin