-
Notifications
You must be signed in to change notification settings - Fork 0
Library: toPrecision()
Eugene Lazutkin edited this page Jul 11, 2018
·
1 revision
toPrecision() module is a function, which rounds up a number to a given precision using toFixed(). To avoid round-off errors it calculates an epsilon value, which depends on an executing system and takes it into account when rounding up.
The function is used to avoid problems like this:
console.log(0.1 + 0.1 + 0.1);
// prints: 0.30000000000000004In a module-enabled environment (like Babel) it can be accessed like that:
import toPrecision from '@researchnow/reno/src/utils/toPrecision';In global-based environments (like a browser) it is frequently mapped to Reno.utils.toPrecision.
It takes the following arguments:
-
numberis a number or a string that represents a number. -
decimalPlacesis an integer specifying how to round up. See toFixed() for details.
It returns a number rounded up as requested.
toPrecision.epsilon is a numeric constant that represents the smallest discernable number, which can be represented on the underlying computer system.