For AI agents: the complete documentation index is available at /tc39-atlas/llms.txt, the full documentation bundle is available at /tc39-atlas/llms-full.txt, and this page is available as Markdown at /tc39-atlas/proposals/proposal-intl-keep-trailing-zeros.md.
  • 简体中文
  • Keep trailing zeros in Intl.NumberFormat and Intl.PluralRules S3

    中文标题:在 Intl.NumberFormat 和 Intl.PluralRules 中保留尾随零

    提案概览
    提案速览

    该提案解决了 Intl.NumberFormat 格式化数字字符串或 Intl.PluralRules.select 使用数字字符串时尾随零丢失的问题,目前这些操作会丢弃尾随零。它建议在内部数学值中保留这些尾随零,从而使格式化 '1.0' 得到 '1.0',并且复数选择将其视为 'other'。

    Note

    以下 README 来自上游仓库,其中的阶段或状态标注可能滞后;当前信息以提案概览为准。

    在 Intl.NumberFormat 和 Intl.PluralRules 中保留尾随零

    状态

    阶段:3

    提案倡导者:Eemeli Aro

    审查者:Richard Gibson (#7) 和 Shane Carr (#8)

    演示文稿:

    Polyfill: eemeli/formatjs-for-keep-trailing-zeros

    动机

    在格式化数字或选择其复数类别时,尾随零很重要, 并且当数字字符串输入值中包含尾随零时应予以保留。

    使用场景

    目前,尾随零会被丢弃:

    const nf = new Intl.NumberFormat("en");
    nf.format("1.0") === "1";
    
    const pr = new Intl.PluralRules("en");
    pr.select("1.0") === "one";

    相反,它们应该被保留:

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

    描述

    目前,Intl.NumberFormat 和 Intl.PluralRules 接受数字字符串作为输入, 在内部将其转换为具有任意十进制精度的 Intl 数学值

    如果该提案被接受,它将改变这些接口的内部机制, 使得尾随零得以保留, 并包含在格式化或选择的值中。 对 Number 或 BigInt 值的处理将不会改变, 并且像 maximumFractionDigits 这样的选项仍将像以前一样工作:

    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 默认值为 3。

    背景

    数字字符串值的处理在 2023 年曾作为 Intl.NumberFormat V3 提案 的一部分进行了更改, 在此之前它们被解析为较低精度的 Number 值。

    Decimal 提案 希望引入能够表示精度高达 34 位小数的数值类型, 以及通过 Decimal.Amount 或其他表示方式对值精度进行单独表示。 目前,Intl.NumberFormat 和 Intl.PluralRules 的有效精度限制 大约是整数部分 300 位小数,小数部分 100 位。 小数精度的最大限制在 2023 年通过 ECMA-402 PR #786 从 20 增加到 100。

    带有尾随零的数字的字符串表示可通过 Number.prototype.toPrecision 获得:

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