Skip to content

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.30000000000000004

Usage

In 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.

toPrecision(number, decimalPlaces)

It takes the following arguments:

  • number is a number or a string that represents a number.
  • decimalPlaces is an integer specifying how to round up. See toFixed() for details.

It returns a number rounded up as requested.

Properties

toPrecision.epsilon is a numeric constant that represents the smallest discernable number, which can be represented on the underlying computer system.

Clone this wiki locally