I have a few values:
var one = 1.0000 var two = 1.1000 var three = 1.1200 var four = 1.1230
and function:
function tofixed(val) { return val.toFixed(2); }
this return:
1.00 1.10 1.12 1.12
LIVE
I want maximum size after dot - 2, but only if numbers after for != 0. So i would like receive:
1 1.1 1.12 1.12
How can i make it?
The "toFixed is not a function" error occurs when the toFixed() method is called on a value that is not a number . To solve the error, either convert the value to a number before calling the toFixed method or only call the method on numbers.
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.
The toFixed() method converts a number to a string. The toFixed() method rounds the string to a specified number of decimals.
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.
.toFixed(x)
returns a string. Just parse it as a float again:
return parseFloat(val.toFixed(2));
http://jsfiddle.net/mblase75/y5nEu/1/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With