# General Translation Platform: formatNum URL: https://generaltranslation.com/zh/docs/platform/core/reference/gt-class-methods/formatting/format-num.mdx --- title: formatNum description: 按区域设置格式化数字、货币、百分比和数值。formatNum 的 API 参考。 --- 在 [GT](/docs/platform/core/reference/gt-class/constructor) 实例上,按照区域设置的惯例格式化数字。General Translation 使用内置的 `Intl.NumberFormat` API,自动处理目标区域设置的小数分隔符、分组分隔符和数字系统。 ## 概览 [#overview] 在 [`GT`](/docs/platform/core/reference/gt-class/constructor) 实例上调用 `formatNum`,传入要格式化的数字和一个可选的选项对象。它会返回格式化后的数字字符串。 ```typescript const gt = new GT({ targetLocale: 'de' }); const formatted = gt.formatNum(1234.56, { style: 'decimal', minimumFractionDigits: 2, }); // "1.234,56"(德语数字格式) ``` 签名: ```typescript formatNum( number: number, options?: { locales?: string | string[] } & Intl.NumberFormatOptions ): string ``` *注意:`formatNum` 在本地通过 `Intl.NumberFormat` 运行,不需要 API 密钥。默认情况下,它会按该实例的目标区域设置进行格式化,并依次回退到源区域设置以及库的默认值 (`en`) ;传入 `locales` 可覆盖此设置。若要在没有 `GT` 实例的情况下进行格式化,请参阅独立版 [`formatNum`](/docs/platform/core/reference/utility-functions/formatting/format-num)。* ## 工作方式 [#how-it-works] * **区域设置解析。** 默认情况下,此方法会使用实例的目标区域设置进行格式化,并依次回退到 源区域设置 和库默认值 (`en`) ——而不是 `locales` 配置数组。若要仅覆盖单次调用,请在选项中传入 `locales`。 * **由 Intl 支持。** 格式化由浏览器原生的 [`Intl.NumberFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat) 处理,因此支持所有标准的 `Intl.NumberFormatOptions`,并会自动应用相应的区域设置惯例。 * **style 要求。** 货币格式化同时需要 `style: 'currency'` 和有效的 `currency` 代码。时间单位格式化同时需要 `style: 'unit'` 和有效的 `unit` 标识符。 ## 参数 [#parameters] | 参数 | 说明 | 类型 | 可选 | 默认值 | | --------------------- | ------------------------------------------------------- | -------- | -- | --- | | [`number`](#number) | 要格式化的数字。 | `number` | 否 | — | | [`options`](#options) | 格式化配置,基于 `Intl.NumberFormatOptions` 扩展,并支持覆盖 `locales`。 | `object` | 是 | — | ### `number` [#number] **类型** `number` · **必填** 要格式化的数字。 ### `options` [#options] **类型** `{ locales?: string | string[] } & Intl.NumberFormatOptions` · **可选** 格式化配置。下表列出了已发布的 Core 类型提供的常用选项及其实际采用的 Core 默认值。有关补充的标准和运行时特定详细信息,请参阅 [`Intl.NumberFormat` 构造函数选项](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#options)。 | 名称 | 描述 | 类型 | 可选 | 默认值 | | -------------------------- | -------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------- | -- | ------------------------------------------ | | `locales` | 覆盖格式化时使用的区域设置。 | `string \| string[]` | 是 | `targetLocale` → `sourceLocale` → `en` | | `localeMatcher` | 区域设置匹配算法。 | `'lookup' \| 'best fit'` | 是 | `'best fit'` | | `numberingSystem` | 数字系统,例如 `latn` 或 `arab`。 | `string` | 是 | `'latn'` | | `style` | 数字格式化样式。 | `'decimal' \| 'currency' \| 'percent' \| 'unit'` | 是 | `'decimal'` | | `currency` | `currency` 代码 (当 `style` 为 `'currency'` 时必需) 。 | `string` | 是 | — | | `currencyDisplay` | 如何显示货币。 | `'symbol' \| 'narrowSymbol' \| 'code' \| 'name'` | 是 | `'symbol'` | | `currencySign` | 要使用的货币符号样式。 | `'standard' \| 'accounting'` | 是 | `'standard'` | | `unit` | 时间单位标识符 (当 `style` 为 `'unit'` 时必需) 。 | `string` | 是 | — | | `unitDisplay` | 如何显示时间单位。 | `'short' \| 'narrow' \| 'long'` | 是 | `'short'` | | `minimumIntegerDigits` | 整数部分的最小位数 (1–21) 。 | `number` | 是 | `1` | | `minimumFractionDigits` | 小数部分的最小位数 (0–100) 。样式会影响默认值。 | `number` | 是 | 小数/百分比为 `0`;货币为货币最小单位位数;使用紧凑默认值时为 `0` | | `maximumFractionDigits` | 小数部分的最大位数 (0–100) 。样式和最小值会影响默认值。 | `number` | 是 | 小数为 `3`;百分比为 `0`;货币为货币最小单位位数;使用紧凑默认值时为 `0` | | `minimumSignificantDigits` | 启用有效数字舍入时的最少有效数字位数 (1–21) 。 | `number` | 是 | `1` | | `maximumSignificantDigits` | 启用有效数字舍入时的最多有效数字位数 (1–21) 。 | `number` | 是 | `21`;使用紧凑默认值时为 `2` | | `roundingPriority` | 小数位和有效数字设置如何相互作用。 | `'auto' \| 'morePrecision' \| 'lessPrecision'` | 是 | `'auto'`;使用紧凑默认值时为 `'morePrecision'` | | `notation` | 数字表示法。 | `'standard' \| 'scientific' \| 'engineering' \| 'compact'` | 是 | `'standard'` | | `compactDisplay` | 紧凑表示法的显示样式。 | `'short' \| 'long'` | 是 | `'short'` | | `useGrouping` | 是否以及何时使用分组分隔符。 | `boolean \| 'always' \| 'auto' \| 'min2'` | 是 | `'auto'`;使用紧凑表示法时为 `'min2'` | | `signDisplay` | 何时显示符号。 | `'auto' \| 'never' \| 'always' \| 'exceptZero' \| 'negative'` | 是 | `'auto'` | | `roundingMode` | 舍入模式。 | `'ceil' \| 'floor' \| 'expand' \| 'trunc' \| 'halfCeil' \| 'halfFloor' \| 'halfExpand' \| 'halfTrunc' \| 'halfEven'` | 是 | `'halfExpand'` | | `roundingIncrement` | 舍入增量。非默认值要求有效的最小和最大小数位数相同,且不能与有效数字舍入或非 `'auto'` 的 `roundingPriority` 结合使用。 | `1 \| 2 \| 5 \| 10 \| 20 \| 25 \| 50 \| 100 \| 200 \| 250 \| 500 \| 1000 \| 2000 \| 2500 \| 5000` | 是 | `1` | | `trailingZeroDisplay` | 是否显示尾随零。 | `'auto' \| 'stripIfInteger'` | 是 | `'auto'` | 当设置 `notation: 'compact'` 且未指定任何小数位数或有效数字选项时,实际生效的位数默认值为 `minimumFractionDigits: 0`、`maximumFractionDigits: 0`、`minimumSignificantDigits: 1` 和 `maximumSignificantDigits: 2`。此时,`roundingPriority` 默认为 `'morePrecision'`,`useGrouping` 默认为 `'min2'`。 ## 返回值 [#returns] **类型** `string` 根据目标区域设置的惯例格式化后的数字。 ## 示例 [#examples] ```typescript import { GT } from 'generaltranslation'; const gt = new GT({ targetLocale: 'en-US' }); // 基本十进制格式化 console.log(gt.formatNum(1234.567)); // 输出:"1,234.567" // 德语区域设置格式化 console.log(gt.formatNum(1234.567, { locales: 'de-DE' })); // 输出:"1.234,567" // 法语区域设置格式化 console.log(gt.formatNum(1234.567, { locales: 'fr-FR' })); // 输出:"1 234,567" ``` ```typescript // 货币格式化 // 美元格式化 console.log(gt.formatNum(1234.56, { style: 'currency', currency: 'USD', })); // Output: "$1,234.56" // 使用德国区域设置的欧元格式化 console.log(gt.formatNum(1234.56, { style: 'currency', currency: 'EUR', locales: 'de-DE', })); // Output: "1.234,56 €" // 货币显示选项 console.log(gt.formatNum(1234.56, { style: 'currency', currency: 'USD', currencyDisplay: 'code', })); // Output: "USD 1,234.56" // 会计格式(负数用括号表示) console.log(gt.formatNum(-1234.56, { style: 'currency', currency: 'USD', currencySign: 'accounting', })); // Output: "($1,234.56)" ``` ```typescript // 百分比和科学计数法 // 基本百分比 console.log(gt.formatNum(0.1234, { style: 'percent' })); // Output: "12%" // 带小数位的百分比 console.log(gt.formatNum(0.1234, { style: 'percent', minimumFractionDigits: 1, maximumFractionDigits: 2, })); // Output: "12.34%" // 紧凑表示法 console.log(gt.formatNum(1234567, { notation: 'compact' })); // Output: "1.2M" // 科学计数法 console.log(gt.formatNum(1234567, { notation: 'scientific' })); // Output: "1.235E6" ``` ## 注意事项 [#notes] * 数字格式会自动遵循区域设置中的惯例。 * 该方法使用浏览器原生的 `Intl.NumberFormat`,以确保性能和准确性。 * 货币格式化需要同时提供 `style: 'currency'` 和有效的 `currency` 代码。 * 时间单位格式化需要同时提供 `style: 'unit'` 和有效的 `unit` 标识符。