GT ClassMethodsFormatting
formatNum
formatNum 方法的 API 参考:根据 locale 习惯格式化数字
概述
formatNum 方法使用国际化 API,按照目标 locale 的本地化约定来格式化数字。
它会基于目标 locale 自动处理小数点、千位分隔符和数字系统。
const gt = new GT({ targetLocale: 'de' });
const formatted = gt.formatNum(1234.56, {
  style: 'decimal',
  minimumFractionDigits: 2
});
// 返回:"1.234,56"(德语数字格式)参考资料
参数
| 名称 | 类型 | 说明 | 
|---|---|---|
| number | number | 要格式化的数值 | 
| options? | NumberFormatOptions | 可选的格式化选项 | 
NumberFormatOptions
在 Intl.NumberFormatOptions 的基础上扩展了额外的 locale 规范:
| 名称 | 类型 | 描述 | 
|---|---|---|
| locales? | string | string[] | 覆盖用于格式化的 locales(默认为实例的 locales) | 
| style? | 'decimal' | 'currency' | 'percent' | 'unit' | 数字格式化样式 | 
| currency? | string | 货币代码(当 style 为 'currency' 时必填) | 
| currencyDisplay? | 'symbol' | 'narrowSymbol' | 'code' | 'name' | 货币的显示方式 | 
| currencySign? | 'standard' | 'accounting' | 货币符号样式 | 
| unit? | string | 单位标识符(当 style 为 'unit' 时必填) | 
| unitDisplay? | 'short' | 'narrow' | 'long' | 单位的显示方式 | 
| minimumIntegerDigits? | number | 最少整数位数(1-21) | 
| minimumFractionDigits? | number | 最少小数位数(0-20) | 
| maximumFractionDigits? | number | 最多小数位数(0-20) | 
| minimumSignificantDigits? | number | 最少有效数字(1-21) | 
| maximumSignificantDigits? | number | 最多有效数字(1-21) | 
| useGrouping? | boolean | 'always' | 'auto' | 'min2' | 是否使用分组分隔符 | 
| notation? | 'standard' | 'scientific' | 'engineering' | 'compact' | 数值记数格式 | 
| compactDisplay? | 'short' | 'long' | 紧凑记数法显示样式 | 
| signDisplay? | 'auto' | 'never' | 'always' | 'exceptZero' | 何时显示符号 | 
| roundingMode? | 'ceil' | 'floor' | 'expand' | 'trunc' | 'halfCeil' | 'halfFloor' | 'halfExpand' | 'halfTrunc' | 'halfEven' | 舍入模式 | 
| roundingIncrement? | 1 | 2 | 5 | 10 | 20 | 25 | 50 | 100 | 舍入增量 | 
| trailingZeroDisplay? | 'auto' | 'stripIfInteger' | 是否显示末尾零 | 
返回值
string - 按照 locale 约定格式化的数字。
示例
基本数字格式
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”货币格式
// 美元格式化
console.log(gt.formatNum(1234.56, {
  style: 'currency',
  currency: 'USD'
}));
// 输出:"$1,234.56"
// 德语 locale 的欧元格式化
console.log(gt.formatNum(1234.56, {
  style: 'currency',
  currency: 'EUR',
  locales: 'de-DE'
}));
// 输出:"1.234,56 €"
// 货币显示选项
console.log(gt.formatNum(1234.56, {
  style: 'currency',
  currency: 'USD',
  currencyDisplay: 'code'
}));
// 输出:"USD 1,234.56"
// 会计格式(负数用括号显示)
console.log(gt.formatNum(-1234.56, {
  style: 'currency',
  currency: 'USD',
  currencySign: 'accounting'
}));
// 输出:"($1,234.56)"百分数与科学计数法
// 基本百分比
console.log(gt.formatNum(0.1234, { style: 'percent' }));
// 输出:“12%”
// 带小数的百分比
console.log(gt.formatNum(0.1234, {
  style: 'percent',
  minimumFractionDigits: 1,
  maximumFractionDigits: 2
}));
// 输出:“12.34%”
// 紧凑表示法
console.log(gt.formatNum(1234567, { notation: 'compact' }));
// 输出:“1.2M”
// 科学计数法
console.log(gt.formatNum(1234567, { notation: 'scientific' }));
// 输出:“1.235E6”注意事项
- 数字格式会自动遵循所选 locale 的本地约定
- 该方法使用浏览器原生的 Intl.NumberFormat,以获得最佳性能与准确性
- 货币格式需同时设置 style: 'currency',并提供有效的currency代码
- 单位格式需同时设置 style: 'unit',并提供有效的unit标识符
后续步骤
- 查看 Intl.NumberFormat文档 以了解更多 options
- 参见 format-currency获取更专业的货币格式化
- 参见 format-message了解带数字插值的消息格式化
- 查看独立的 format-num,用于在没有 GT 实例时使用
- 参见 get-locale-properties以获取特定 locale 的格式化信息
这份指南怎么样?

