Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

javascript percentage rounding

I have a value of 0.9019497053624151. It needs to display 90, so it is ideal to use the ff formula? by the way this is a percentage

console.log(Math.round(0.9019497053624151 * 10) / 10 * 100);

I tried rounding it off to 1 decimal place and then * 100, should I round it to 1 or 2 decimal places?

What do you call this percentage rounding? That from a value of 0.9019497053624151% the expected result is 90%

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use the toFixed method (see documentation here)

You can specify the number of decimal you want. Example :

console.log(0.9119497053624151.toFixed(2));
console.log(0.9119497053624151.toFixed(4));
console.log(0.9119497053624151.toFixed(1));
console.log(0.9.toFixed(2));

You can then easely multiply it by 100 as you want 🙂

myNumber.toFixed(2) * 100

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading