Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.toFixed not for .0*

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?

like image 216
user2565589 Avatar asked Jul 09 '13 18:07

user2565589


People also ask

Why is toFixed not working?

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.

Does .toFixed round up?

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.

What does toFixed mean?

The toFixed() method converts a number to a string. The toFixed() method rounds the string to a specified number of decimals.

Does to fixed round?

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.


1 Answers

.toFixed(x) returns a string. Just parse it as a float again:

return parseFloat(val.toFixed(2)); 

http://jsfiddle.net/mblase75/y5nEu/1/

like image 183
Blazemonger Avatar answered Oct 05 '22 15:10

Blazemonger