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

round to the nearest tenth

Please how to round to the nearest decimal tenth?
Example : 0.56 => 0.5; 2.78 => 2.8

>Solution :

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

Multiply by 10, then round then divide back by 10:

const round = (num) => Math.round(num * 10)/10

console.log(round(0.56))
console.log(round(2.78))

Then using the same logic, you can write a general rounding function:

const round = (num, decimal) => Math.round(num * 10**decimal)/10**decimal

console.log(round(5.142533564, 4))
console.log(round(62.5236, 2))
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