I am looking for an efficient way to cut floating number at Javascript which are long. I need this because my output for taking percentage of two numbers can be sometimes like 99.4444444 while I am only interested in the first 2 digits after "." such as 99.44
My current percentage taking function for 2 numbers:
function takePercentage(x,y){
return (x /y) * 100;
}
I am looking for an efficient way to cut floating number at Javascript which are long. I need this because my output for taking percentage of two numbers can be sometimes like 99.4444444 while I am only interested in the first 2 digits after "." such as 99.44
My current percentage taking function for 2 numbers:
function takePercentage(x,y){
return (x /y) * 100;
}
Share
Improve this question
edited Jan 3, 2015 at 22:23
Stephan Muller
27.6k18 gold badges86 silver badges127 bronze badges
asked Sep 22, 2009 at 15:38
HellnarHellnar
65k82 gold badges208 silver badges282 bronze badges
3 Answers
Reset to default 7You can use number.toFixed:
function takePercentage(x,y){
return ((x /y) * 100).toFixed(2);
}
How about this:
Math.round( myfloatvalue*100 ) / 100
function takePercentage(x,y){
n = (x /y) * 100;
return n.toPrecision(2);
}
That should do it!
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744842262a4596629.html
评论列表(0条)