85

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;
}
1
  • 4
    Note: .toFixed(x) will return a string object Commented Feb 8, 2011 at 19:18

7 Answers 7

231

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

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

should fix things up.

5
  • Can't you also just multiply it by 1?
    – kaleazy
    Commented Sep 6, 2014 at 10:51
  • 5
    I think it should be value = parseFloat(value.toFixed(2)); That is, unless you want a string for the value. ToFixed returns a string, doing the toFixed inside will keep the value a number.
    – dchin
    Commented Oct 23, 2014 at 15:39
  • the value is being assigned to an input form field, which is ALWAYS a string.
    – Marc B
    Commented Oct 23, 2014 at 16:21
  • @dchin makes a valid point... you should do it this way if you need a number returned..
    – 72GM
    Commented Feb 19, 2015 at 11:32
  • @dchin if you want trailing 0's to a certain place, for example to the hundredths or thousandths, you do want it as a string to retain that many digits. For example a counter will look more stable if the number of digits does not change. parseFloat would most likely cause you to lose your trailing digits if you leave it as a number, undoing the "3 decimal place" placeholder 0 digits, right?
    – OG Sean
    Commented Aug 31, 2017 at 0:38
10

I tried function toFixed(2) many times. Every time console shows "toFixed() is not a function".

but how I resolved is By using Math.round()

eg:

if ($(this).attr('name') == 'time') {
    var value = parseFloat($(this).val());
    value = Math.round(value*100)/100; // 10 defines 1 decimals, 100 for 2, 1000 for 3
    alert(value);
}

this thing surely works for me and it might help you guys too...

3

Your value must not be an integer so add 0 to convert your value into integer and then toFixed() will work.

2

Before rounding of the decimal values to 2 number. Convert the value into number simple like below

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

This should fix things.

1

Example simple (worked):

var a=Number.parseFloat($("#budget_project").val()); // from input field
var b=Number.parseFloat(html); // from ajax
var c=a-b;
$("#result").html(c.toFixed(2)); // put to id='result' (div or others)
0

Your conversion data is response[25] and follow the below steps.

var i = parseFloat(response[25]).toFixed(2)
console.log(i)//-6527.34
0
document.getElementById("EDTVALOR").addEventListener("change", function() {
  this.value = this.value.replace(",", ".");
  this.value = parseFloat(this.value).toFixed(2);
  if (this.value < 0) {
    this.value = 0;
  }
  this.value = this.value.replace(".", ",");
  this.value = this.value.replace("NaN", "0");
});
1
  • "The specified value "0,01" cannot be parsed, or is out of range."
    – user5713188
    Commented Dec 10, 2021 at 9:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.