Why is my toFixed() function not working?

JavascriptJqueryNumber Formatting

Javascript Problem Overview


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;
}

Javascript Solutions


Solution 1 - Javascript

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

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

should fix things up.

Solution 2 - Javascript

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...

Solution 3 - Javascript

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

Solution 4 - Javascript

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)

Solution 5 - Javascript

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

var i = parseFloat(response[25]).toFixed(2)
console.log(i)//-6527.34

Solution 6 - Javascript

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");
});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBenView Question on Stackoverflow
Solution 1 - JavascriptMarc BView Answer on Stackoverflow
Solution 2 - JavascriptPulath YaseenView Answer on Stackoverflow
Solution 3 - JavascriptShamsView Answer on Stackoverflow
Solution 4 - JavascriptVladView Answer on Stackoverflow
Solution 5 - JavascriptKARTHIKEYAN.AView Answer on Stackoverflow
Solution 6 - JavascriptMauricio ParizottoView Answer on Stackoverflow