Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my toFixed() function not working?

Here's the relevant code. I've confirmed with the alert that the correct number is saved, it's just not being changed to 2 decimal places.

if ($(this).attr('name') == 'time') {     var value = $(this).val();     parseFloat(value).toFixed(2);     alert(value);     editEntry.time = value; } 
like image 778
Ben Avatar asked Feb 08 '11 19:02

Ben


People also ask

How do I use toFixed in JavaScript?

html. The toFixed() method in JavaScript is used to format a number using fixed-point notation. It can be used to format a number with a specific number of digits to the right of the decimal. The toFixed() method is used with a number as shown in above syntax using the '.

What is the toFixed () function used for?

Description. toFixed() returns a string representation of numObj that does not use exponential notation and has exactly digits digits after the decimal place. The number is rounded if necessary, and the fractional part is padded with zeros if necessary so that it has the specified length.

Does JavaScript toFixed round?

Note. The toFixed() method will round the resulting value if necessary. The toFixed() method will pad the resulting value with 0's if there are not enough decimal places in the original number. The toFixed() method does not change the value of the original number.

How do you round a number in JavaScript?

The Math. round() method rounds a number to the nearest integer. 2.49 will be rounded down (2), and 2.5 will be rounded up (3).


1 Answers

You're not assigning the parsed float back to your value var:

value = parseFloat(value).toFixed(2); 

should fix things up.

like image 89
Marc B Avatar answered Sep 21 '22 15:09

Marc B