If value is between -1 and 1, returns value rounded to one of the following decimal places (whichever is smaller):
8
number of decimal places to reach value's first significant digit + buffer
Otherwise returns value rounded to the smaller of buffer and decimals
The returned value will have a minimum of 2 decimal places.
Example
handleSignificantDecimals(0.123456789, 2, 3) // '0.123' - 0 decimal place to reach first significant digit, then buffer of 3 handleSignificantDecimals(0.0123456789, 2, 3) // '0.0123' - 1 decimal places to reach first significant digit, then buffer of 3 handleSignificantDecimals(0.123456789, 2, 4) // '0.1235' - 0 decimal place to reach first significant digit, then buffer of 4 handleSignificantDecimals(0.0123456789, 2, 4) // '0.01235' - 1 decimal places to reach first significant digit, then buffer of 4 handleSignificantDecimals(0.000000123456789, 2, 3) // '0.00000012' - 8 decimal limit reached handleSignificantDecimals(0.00000000123456, 2, 3) // '0.00' - 8 decimal limit reached, rounded to 0, formatted to 2 decimal places
handleSignificantDecimals(2.1234, 2, 1) // '2.10' handleSignificantDecimals(2.1234, 1, 2) // '2.10' - this and the above are both Math.min(2, 1) decimals
If
value
is between -1 and 1, returnsvalue
rounded to one of the following decimal places (whichever is smaller):value
's first significant digit +buffer
Otherwise returns
value
rounded to the smaller ofbuffer
anddecimals
The returned value will have a minimum of 2 decimal places.
Example