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
How would I achieve it on javascript?
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))