floating point - shortening Javascript number - Stack Overflow

I am looking for an efficient way to cut floating number at Javascript which are long. I need this beca

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
Add a ment  | 

3 Answers 3

Reset to default 7

You 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

相关推荐

  • floating point - shortening Javascript number - Stack Overflow

    I am looking for an efficient way to cut floating number at Javascript which are long. I need this beca

    2天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信