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

I want my value modified to ALWAYS have three decimal digits

My logic calculates and returns a value based on user input, I want to modify that value to always have three decimal digits

For example;

1 to 1.000
1.02 to 1.020
2.000004 to 2.000
2.5687 to 2.569

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

How would I achieve it on javascript?

>Solution :

You can use Number().toFixed() to do it

const formatVal = (val,precise = 3) =>{
  return Number(val).toFixed(precise)
}

console.log(formatVal(1,3))
console.log(formatVal(1.02,3))
console.log(formatVal(2.000004,3))
console.log(formatVal(2.5687))
console.log("-----------------")
console.log(formatVal(2.5687,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