Skip to content

tc39/proposal-intl-keep-trailing-zeros

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Keep trailing zeros in Intl.NumberFormat and Intl.PluralRules

Status

Champion: Eemeli Aro

Stage: 1

Presentations:

Motivation

Trailing zeros are important when formatting numbers or selecting their plural categories, and should be retained when included in a numeric string input value.

Use cases

Currently, trailing zeros are discarded:

const nf = new Intl.NumberFormat("en");
nf.format("1.0") === "1";

const pr = new Intl.PluralRules("en");
pr.select("1.0") === "one";

Instead, they should be retained:

const nf = new Intl.NumberFormat("en");
nf.format("1.0") === "1.0";

const pr = new Intl.PluralRules("en");
pr.select("1.0") === "other";

Description

Currently, Intl.NumberFormat and Intl.PluralRules accept numeric strings as input, converting such internally to an Intl Mathematical Value with arbitrary decimal precision.

If accepted, this proposal would change the internals of these interfaces such that trailing zeros would be retained, and included in the formatted or selected value. The treatment of Number or BigInt values would not change, and options such as maximumFractionDigits would still work as before:

const nf = new Intl.NumberFormat('en', { minimumFractionDigits: 1 });

nf.format('1') === '1.0'
nf.format('1.00') === '1.00'
nf.format('1.0000') === '1.000'
  // maximumFractionDigits default is 3.

Background

The treatment of numeric string values was previously changed in 2023 as a part of the Intl.NumberFormat V3 proposal, before which they were parsed into lower-precision Number values.

The Decimal proposal is looking to introduce a numeric type capable of representing values with up to 34 decimal places of precision, along with a separate representation of the value's precision via Decimal.Amount or some other representation. Currently, the effective limits for the precision of Intl.NumberFormat and Intl.PluralRules are around 300 decimal places for the integer part, and 100 places for the fraction part. The maximum limit for the fraction precision was increased in 2023 from 20 to 100 in ECMA-402 PR #786.

A numerical string representation of a Number with trailing zeros is available as Number.prototype.toPrecision:

(42).toPrecision(4) === "42.00"
(4200).toPrecision(2) === "4.2e+3"

About

No description, website, or topics provided.

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks