You're not assigning the parsed float back to your value var:
value = parseFloat(value).toFixed(2);
should fix things up.
Answer from Marc B on Stack OverflowYou're not assigning the parsed float back to your value var:
value = parseFloat(value).toFixed(2);
should fix things up.
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...
toFixed isn't a method of non-numeric variable types. In other words, Low and High can't be fixed because when you get the value of something in Javascript, it automatically is set to a string type. Using parseFloat() (or parseInt() with a radix, if it's an integer) will allow you to convert different variable types to numbers which will enable the toFixed() function to work.
var Low = parseFloat($SliderValFrom.val()),
High = parseFloat($SliderValTo.val());
That is because Low is a string.
.toFixed() only works with a number.
Try doing:
Low = parseFloat(Low).toFixed(..);
toFixed() method formats a number. The current value is of type string and instead of arithmetic addition, string concatenation is happening. Convert those to number before adding:
Change:
var totalSum = (grandTotal + getShippingCost).toFixed(3);
To
var totalSum = (Number(grandTotal) + Number(getShippingCost)).toFixed(3);
toFixed method is not available on non-number values. you need to parse value to Number first than you can use toFixed method.
let str = `123.123456`
console.log(Number(str).toFixed(3))
console.error(str.toFixed(3))
Does anyone know what is this error?? I think it’s related to the date format or something like that. Note that the row hyperlink doesn’t take me to the error place!!
I think the easiest way to prevent the error from happening is to always parse the parameter as number:
var total = 0;
function add(a){
total+=a;
var float_num = Number(total).toFixed(2);
return float_num;
}
Check if any of the code above has redefined the toFixed on the Number prototype, For instance
Number.prototype.toFixed = {};
var total = 0;
function add(a) {
total += a;
var float_num = total.toFixed(2);
return float_num;
}
add(2)
is one way to replicate the error.