Truncate a number to decimal places without rounding

Cut the decimal representation after a number of digits.

JavaScript Version 1.0.0 · MIT · .md

truncate-a-number-to-a-given-number-of-decimal-places-without-rounding.js
function toFixedDown({ n, digits }) { return Number(`${n}`.match(new RegExp(`^-?\\d+(?:\\.\\d{0,${digits}})?`))[0]); }

Input & expected output

Input

{
  "n": 25.198726354,
  "digits": 2
}

Expected output

25.19

The edge case

Input

{
  "n": -1.999,
  "digits": 1
}

Expected output

-1.9

Know the limits

  • Works on the decimal string, so floating-point drift does not round the result up.
  • Numbers that print in exponent form, such as 1e-7, are outside this contract.

The function above is the implementation used by the lab. The lab validates JSON types and caps size before execution; include equivalent validation when accepting untrusted input in your application.

Source & maintenance

Based on Truncate a number to a given number of decimal places without rounding from the original 1loc collection by Nguyen Huu Phuoc, with onelifemedia. Copyright (c) 2020 phuoc-ng. MIT licensed.

Keeps the string-matching variant and returns a number; rejects exponent notation. Original MDX retained as inert source evidence.

Maintained by Oveyon. Source commit: 14b481cf60df. Read the license.

Local fixture scope · Node.js 22.23.2 · reviewed October 28, 2026. No external service is required.

Keep exploring

Type two or more characters. Try “duplicate”, “webhook” or “retry”.