javascriptdecimalprecision

toPrecision is adding up trailing zeros after the decimal point to total numbers intended


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:

  1. 4828.39019785 -> 4828.3902 (intended)
  2. 0.39019785 -> 0.39019785 (Although it returns total 9 digits, but it kinda work)
  3. 0.0039019785 -> 0.0039019785 (Not intended, returning 11 digits)

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:

  1. 0.39019785 -> 0.3901978 (exactly 8 digits including 0)
  2. 0.0039019785 -> 0.0039019 (exactly 8 digits including 3 zeros)

Solution

  • 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!