问题
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:
You're not assigning the parsed float back to your value var:
value = parseFloat(value).toFixed(2);
should fix things up.
回答2:
Your conversion data is response[25] and follow the below steps.
var i = parseFloat(response[25]).toFixed(2)
console.log(i)//-6527.34
回答3:
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)
回答4:
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");
});
来源:https://stackoverflow.com/questions/4937251/why-is-my-tofixed-function-not-working