After a long hour search, I'm asking this question.
I've a mission to restrict number to just 8 numbers in total. toPrecision is working as intended except this:
Some test cases:
It seems that toPrecision isn't working what it intended i.e to return the exact number we wanted. It adds up the zeros before the decimal and trailing zeros after the decimal into the total number of digits.
My intention:
The toPrecision()
function is designed to limit the number of significant digits, not the number of total digits. It won't produce what you want when there're leading 0's.
So I created a function which does what you need, and gets rid of the extra digits:
var num1 = 0.39019785;
var num2 = 0.0039019785;
const exactPrecision = (number, precision) =>
number
.toPrecision(precision)
.replace(new RegExp("((\\d\\.*){"+precision+"}).*"), '$1');
console.log(exactPrecision(num1, 8))
console.log(exactPrecision(num2, 8))
Hope that helps!