How Lodash toFinite Rounds Scientific Notation

When parsing strings containing scientific notation, Lodash’s _.toFinite function relies on JavaScript's native string-to-number coercion rules coupled with a finite boundary guard. This process subjects input values to IEEE 754 double-precision floating-point rounding—specifically the "round to nearest, ties to even" algorithm—while rounding numbers that exceed maximum floating-point limits to the largest representable finite value.

Underlying Parsing Mechanism

The _.toFinite method does not implement a custom parser for decimal strings. Instead, it delegates string conversion to Lodash's internal toNumber function, which processes strings formatted in scientific notation (such as "1.23456789012345678e+5") using standard ECMAScript numeric conversion rules identical to Number(string).

Round-to-Nearest, Ties-to-Even

Because numeric values in JavaScript are 64-bit binary floating-point numbers (IEEE 754), any scientific notation string parsed by _.toFinite has a precision limit of 53 significand bits (approximately 15 to 17 decimal digits of precision).

When a scientific notation string contains more precision than 53 bits can represent, mathematical rounding occurs during conversion:

  1. Nearest Representable Value: The parser rounds the decimal fraction to the closest 64-bit binary float representation.
  2. Tie-Breaking Rule (Ties to Even): If a decimal value sits precisely halfway between two representable binary floating-point numbers, the value rounds toward the representation with an even least significant bit (zero in the final bit of the mantissa).

Underflow to Zero

For extremely small numbers specified in scientific notation (e.g., "1e-325"), the exponent may fall below the smallest representable subnormal number (Number.MIN_VALUE, approximately 5e-324). In this case, standard IEEE 754 underflow rules apply, and the value mathematically rounds down to 0 (or -0 if negative).

Overflow Clamping (Lodash Clamping)

The unique rounding characteristic of _.toFinite occurs when a scientific notation string specifies a value that exceeds standard numeric limits:

In summary, _.toFinite applies IEEE 754 "round to nearest, ties to even" rounding to the significant digits of scientific notation strings, underflows sub-minimal magnitudes to 0, and clamps exponent overflows to Number.MAX_VALUE.