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/year/pending/proposal-intl-sequence-units.md.
  • 简体中文
  • Intl Sequence Units S1

    中文标题:Intl 序列单位

    提案概览
    提案速览

    该提案规定了在 Intl.NumberFormat 中格式化测量单位序列(例如英尺和英寸、米和厘米)的机制。它通过由 -and- 分隔符连接的复合单位标识符扩展 Intl.NumberFormat,并让 format 和 formatToParts 接受一个数组,其元素依次对应各个子单位。该提案为序列单位定义了验证和错误处理,并使用 Intl.ListFormat 组合格式化后的组成部分,同时提供与 Intl Unit Protocol 和 Amount 提案的集成点。

    Note

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

    Intl 序列单位

    本提案规定了一种在 Intl.NumberFormat 中格式化测量单位序列(例如“英尺和英寸”或“米和厘米”)的机制。

    报告:

    • 第 114 次 TC39 会议(2026 年 5 月):第一阶段和第二阶段 - 幻灯片 | 会议记录
    • 第 115 次 TC39 会议(2026 年 7 月):幻灯片
    • 第 116 次 TC39 会议(2026 年 9 月):第二阶段更新 - 幻灯片
    • 第 117 次 TC39 会议(2026 年 11 月):第二阶段更新 - 幻灯片

    动机

    测量系统经常连续使用多个单位来表示单个量值。示例包括:

    • 人的身高:“5 英尺 11 英寸”或“1 米 80 厘米”
    • 质量:“2 磅 4 盎司”

    当前 ECMAScript 实现需要手动组合多个 Intl.NumberFormat 的输出。这种方法在本地化分隔符、单位顺序和复数一致性方面带来风险,而这些在不同区域设置之间差异很大。本提案通过为多单位格式化提供标准化接口来解决这些需求。

    例如,如今要格式化“5 英尺 11 英寸”,开发者必须编写:

    const feetNf = new Intl.NumberFormat("en", { style: "unit", unit: "foot" });
    const inchNf = new Intl.NumberFormat("en", { style: "unit", unit: "inch" });
    const lf = new Intl.ListFormat("en", { type: "unit" });
    
    lf.format([feetNf.format(5), inchNf.format(11)]);
    // "5 feet, 11 inches"

    虽然这可行,但不够易用,并且容易出现用户错误:

    • 它需要创建和管理多个 Intl.NumberFormat 实例。
    • 开发者必须手动确保正确的单位顺序。
    • 它不会自动处理整个序列的符号显示(例如,仅在第一个单位上显示负号)。

    提议的解决方案

    Intl.NumberFormat 被扩展为支持由 -and- 分隔符连接的复合单位标识符(例如 foot-and-inch、meter-and-centimeter、pound-and-ounce)。

    当指定序列单位时,format 和 formatToParts 接受一个数组(或可迭代对象,见 #19)作为输入。数组中的每个元素必须按顺序对应序列中标识的一个子单位。

    示例

    const nf = new Intl.NumberFormat('en-US', {
      style: 'unit',
      unit: 'foot-and-inch',
    });
    
    // "5 feet, 11 inches"
    nf.format([5, 11]); 
    
    // "-5 feet, 11 inches" (Only the first unit renders the minus sign)
    nf.format([-5, -11]); 
    
    const massNf = new Intl.NumberFormat('en-US', {
      style: 'unit',
      unit: 'pound-and-ounce',
      unitDisplay: 'long'
    });
    
    // "2 pounds, 4 ounces" (Actual output is locale-dependent)
    massNf.format([2, 4]);

    技术语义

    序列单位的格式化过程遵循以下步骤:

    1. 验证:序列必须是受认可单位的有效组合,这些单位测量同一量值并按量值降序排列。受认可的序列在规范中明确列出(例如 foot-and-inch、kilogram-and-gram)。注意,时间单位被排除在序列单位之外,因为它们由 Intl.DurationFormat 管理。有效数字舍入选项(minimumSignificantDigits、maximumSignificantDigits 或 roundingPriority 值不是 "auto",由 [[RoundingType]] 跟踪)不允许用于序列单位,并会在构造期间抛出 RangeError。
    2. 提取:按顺序从输入数组中获取值。
    3. 组成部分格式化:
      • 中间单位:必须是整数,并按整数格式化。
      • 末尾单位:根据 Intl.NumberFormat 实例上配置的小数位数和舍入设置进行格式化。
    4. 组合:使用 Intl.ListFormat(type: "unit")以及指定的 unitDisplay 样式,将得到的组成部分字符串连接起来,以确保符合区域设置习惯的连接词和间距。

    错误处理

    format 或 formatToParts 的输入必须是一个数组,其长度与单位标识符中定义的子单位数量匹配;否则会抛出 RangeError。如果序列中的任何元素是 undefined,则立即抛出 TypeError。

    读取所有元素并转换为数字后,会执行两项最终验证:

    1. 混合符号:所有子单位必须具有相同的符号。混合正值和负值(例如 [5, -11])会抛出 RangeError。零值(0 和 -0)在此检查中既不被视为正数也不被视为负数,并且可以与任一符号一起出现。由于只有第一个子单位会渲染负号(并且后续子单位中的负号——包括 -0 上的负号——会被去除),当第一个子单位为零时,使用开头的 -0 来显示负号(例如 [-0, -6] 格式化为 "-0 feet, 6 inches",而 [0, -6] 会省略负号)。
    2. 中间整数:所有中间子单位(除最后一个之外的所有子单位)必须是整数。提供非整数中间值(例如 [5.5, 6])会抛出 RangeError。
    // Throws RangeError: invalid unit sequence (not in a sanctioned group or order)
    new Intl.NumberFormat('en-US', { style: 'unit', unit: 'meter-and-foot' });
    
    // Throws RangeError: significant digit options are disallowed with sequence units
    new Intl.NumberFormat('en-US', { style: 'unit', unit: 'foot-and-inch', maximumSignificantDigits: 2 });
    
    const nf = new Intl.NumberFormat('en-US', {
      style: 'unit',
      unit: 'foot-and-inch',
    });
    
    // Throws RangeError: array length does not match the unit sequence
    nf.format([5]); 
    
    // Throws RangeError: sub-units have mixed signs
    nf.format([5, -11]);
    
    // Throws RangeError: intermediate sub-unit is not an integer
    nf.format([5.5, 6]);

    与 Intl Unit Protocol 的集成

    本提案与 Intl Unit Protocol 兼容。使用该协议时,value 属性接受子单位量值组成的数组:

    const nf = new Intl.NumberFormat('en-US');
    
    nf.format({ 
      unit: "foot-and-inch", 
      value: [6, 4] 
    });

    与 Amount 提案的集成

    本提案与 Amount 提案保持一致。为支持序列单位,Amount 实例接受由子单位量值组成的数组(例如 new Amount([2, 30], 'hour-and-minute')),而不是标量数值。

    先前技术

    序列单位的格式化要求定义在 Unicode Technical Standard #35 (LDML) 的 单位序列(混合单位) 一节中。TR35 规定使用本地化的 listPattern 数据来组合这些序列,本提案为 ECMAScript 实现了这一点。

    未来方向

    其他单位

    一些常用于序列的单位,例如 arcminute 和 arcsecond,目前尚未在 ECMA-402 中获得认可。格式化像“5° 30′ 12″”这样的角度测量值(概念上会使用 degree-and-arcminute-and-arcsecond)将需要另一个提案,先将 arcminute 和 arcsecond 添加到受认可的单一单位标识符列表中,然后才能将它们用作序列单位。

    考虑过的替代方案

    使用自动分区的标量输入

    接受单个标量值(例如 6.5)并自动将其分区为序列单位(例如 "6 feet, 6 inches")的可能性经过了评估,并因以下原因被拒绝:

    • 输入歧义:标量输入本身并不定义它们代表序列中的哪个子单位(例如,在 foot-and-inch 中,不清楚 6.5 指的是英尺还是英寸)。
    • 转换数据不完整:引擎可能不具备所有可能单位组合的转换因子,尤其是对于自定义或未来的单位。这排除了可靠的自动分区。
    • 精度和舍入:通过浮点运算进行自动分区可能引入错误。要求预先分区的输入可确保输出准确反映开发者的数据。