Javascript Number Reduction

I am looking for an effective way to cut floating-point numbers in Javascript that are long. I need this because my conclusion to get the percentage of two numbers can sometimes be like 99.4444444, while I'm only interested in the first 2 digits after the "." such as 99.44

My current percentage for two numbers:

function takePercentage(x,y){
     return (x /y) * 100;
}
+3
source share
3 answers

How about this:

Math.round( myfloatvalue*100 ) / 100
+2
source

You can use the number. toFixed :

function takePercentage(x,y){
     return ((x /y) * 100).toFixed(2);
}
+7
source
function takePercentage(x,y){
     n = (x /y) * 100;
     return n.toPrecision(2);
}

That should do it!

+2
source

Source: https://habr.com/ru/post/1718298/


All Articles