I need a round to two number after comma. example
5000.0000 to 5000
5000.123 to 5000.12
5000.136 to 5000.13
how do this?
I need function x.toFixed(2); but if at the end of two zero, then they should not show
I need a round to two number after comma. example
5000.0000 to 5000
5000.123 to 5000.12
5000.136 to 5000.13
how do this?
I need function x.toFixed(2); but if at the end of two zero, then they should not show
You can use this javascript function to round the number
function roundNumber(rnum, rlength) {
var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
return parseFloat(newnumber);
}
var num = roundNumber(5000.0000,0); //will return 5000
As @freakish suggests, toFixed
is good idea to round numbers. If you want to floor it, I suggest
parseInt(5000.136*100)/100;
Since x.toFixed(2)
returns a string you may do something like this:
function cut(str) {
if (str[str.length-1] == "0")
return str.substr(0, str.length-1);
if (str[str.length-1] == ".")
return str.substr(0, str.length-1);
return str;
}
x = cut(cut(cut(x.toFixed(2))));
Not the most elegant (for example you could add the function to string's prototype), but definetly working.
This link can help you
http://www.w3schools.com/jsref/jsref_tofixed.asp
Example
Convert a number into a string, keeping only two decimals:
var num = 5.56789; var n=num.toFixed(2);
The result of n will be:
5.57