Cldr.Math.round_significant

You're seeing just the function round_significant, go back to Cldr.Math module for more information.
Link to this function

round_significant(number, n)

View Source

Specs

round_significant(number_or_decimal(), integer()) :: number_or_decimal()

Rounds a number to a specified number of significant digits.

This is not the same as rounding fractional digits which is performed by Decimal.round/2 and Float.round

  • number is a float, integer or Decimal

  • n is the number of significant digits to which the number should be rounded

Examples

iex> Cldr.Math.round_significant(3.14159, 3)
3.14

iex> Cldr.Math.round_significant(10.3554, 1)
10.0

iex> Cldr.Math.round_significant(0.00035, 1)
0.0004

iex> Cldr.Math.round_significant(Decimal.from_float(3.342742283480345e27), 7)
#Decimal<3.342742E+27>

Notes about precision

Since floats cannot accurately represent all decimal numbers, so rounding to significant digits for a float cannot always return the expected results. For example:

=> Cldr.Math.round_significant(3.342742283480345e27, 7)
Expected result:  3.342742e27
Actual result: 3.3427420000000003e27

Use of Decimal numbers avoids this issue:

=> Cldr.Math.round_significant(Decimal.from_float(3.342742283480345e27), 7)
Expected result:  #Decimal<3.342742E+27>
Actual result: #Decimal<3.342742E+27>

More on significant digits

  • 3.14159 has six significant digits (all the numbers give you useful information)

  • 1000 has one significant digit (only the 1 is interesting; you don't know anything for sure about the hundreds, tens, or units places; the zeroes may just be placeholders; they may have rounded something off to get this value)

  • 1000.0 has five significant digits (the ".0" tells us something interesting about the presumed accuracy of the measurement being made: that the measurement is accurate to the tenths place, but that there happen to be zero tenths)

  • 0.00035 has two significant digits (only the 3 and 5 tell us something; the other zeroes are placeholders, only providing information about relative size)

  • 0.000350 has three significant digits (that last zero tells us that the measurement was made accurate to that last digit, which just happened to have a value of zero)

  • 1006 has four significant digits (the 1 and 6 are interesting, and we have to count the zeroes, because they're between the two interesting numbers)

  • 560 has two significant digits (the last zero is just a placeholder)

  • 560.0 has four significant digits (the zero in the tenths place means that the measurement was made accurate to the tenths place, and that there just happen to be zero tenths; the 5 and 6 give useful information, and the other zero is between significant digits, and must therefore also be counted)

Many thanks to Stackoverflow