Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shortening Javascript number

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;
}
like image 443
Hellnar Avatar asked Mar 26 '26 02:03

Hellnar


2 Answers

You can use number.toFixed:

function takePercentage(x,y){
     return ((x /y) * 100).toFixed(2);
}
like image 97
Christian C. Salvadó Avatar answered Mar 28 '26 14:03

Christian C. Salvadó


How about this:

Math.round( myfloatvalue*100 ) / 100
like image 27
cg. Avatar answered Mar 28 '26 14:03

cg.