NumberFormatter

NumberFormatter::create(string $locale, int $style, ?string $pattern = null): ?NumberFormatter

ICU 数字/货币/百分比格式化绑定:按 locale 与样式(DECIMAL/CURRENCY/PERCENT/SPELLL 等)输出本地化数字串,parse() 可逆。示例固定值 en_US 断言 1,234.56——纯数字模式在 ICU 各版本间稳定;货币符号漂移大,只入 notes 不入代码。

C C23

C23
#include <locale.h>
#include <stdio.h>

int main(void) {
    /* C 标准库没有 locale 感知的数字格式化 API;POSIX 的 ' 标志按 LC_NUMERIC
       分组,而 C locale 根本没有定义分组符 */
    setlocale(LC_NUMERIC, "C");
    printf("%'.2f\n", 1234.56); /* -> "1234.56":C locale 没有可插入的分组符 */
    printf("%.2f\n", 1234.56);  /* 普通转换永远与 locale 无关 */
    return 0;
}
输出
1234.56
1234.56

C++ C++26

C++26
#include <clocale>
#include <cstdio>

int main() {
    // 与 C 相同:' 标志按 LC_NUMERIC 分组;C locale 没有分组符
    std::setlocale(LC_NUMERIC, "C");
    std::printf("%'.2f\n", 1234.56); // -> "1234.56"
    return 0;
}
输出
1234.56

C# v11.0.100

v11.0.100
// "N2" 是定精度分组模式;符号(分组符/小数点)由 culture 提供
using System.Globalization;

Console.WriteLine((1234.56).ToString("N2", CultureInfo.GetCultureInfo("en-US")));
Console.WriteLine((1234.56).ToString("N2", CultureInfo.GetCultureInfo("de-DE")));
输出
1,234.56
1.234,56

Go v1.27.4

v1.27.4
package main

import "fmt"

func main() {
	// 标准库格式化与 locale 无关;人性化分组要用 golang.org/x/text
	fmt.Printf("%.2f\n", 1234.56)
}
输出
1234.56

Java v27.0.1

v27.0.1
import java.text.NumberFormat;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        // NumberFormat 是 ICU NumberFormatter 的 JDK 原生姊妹
        System.out.println(NumberFormat.getNumberInstance(Locale.US).format(1234.56));
        System.out.println(NumberFormat.getNumberInstance(Locale.GERMANY).format(1234.56));
    }
}
输出
1,234.56
1.234,56

JavaScript v26.13.1

v26.13.1
// Intl.NumberFormat 与 PHP 的 NumberFormatter 同属 ICU 家族
console.log(new Intl.NumberFormat('en-US').format(1234.56));
console.log(new Intl.NumberFormat('de-DE').format(1234.56));
输出
1,234.56
1.234,56

PHP v8.6.0

v8.6.0
// NumberFormatter 绑定 ICU:每个 locale 一个格式化器,样式决定模式
$n = new NumberFormatter('en_US', NumberFormatter::DECIMAL);
echo $n->format(1234.56), "\n";   // en_US:逗号分组、点号小数
echo $n->parse('1,234.56'), "\n"; // 往返:locale 字符串解析回数值
$de = new NumberFormatter('de_DE', NumberFormatter::DECIMAL);
echo $de->format(1234.56), "\n";  // de_DE:点号分组、逗号小数
输出
1,234.56
1234.56
1.234,56

Python v3.15.2

v3.15.2
# 标准库格式化与 locale 无关:分隔符由格式规范决定
print(format(1234.56, ',.2f'))  # 逗号分组
print(format(1234.56, '_.2f'))  # 下划线分组
# locale 感知格式化需要 locale 模块 + OS 安装的 locale(见 notes);
# 第三方库 Babel 才是对标 NumberFormatter 的 ICU 方案
输出
1,234.56
1_234.56

Rust v1.99.1

v1.99.1
fn main() {
    // 标准库格式化与 locale 无关;ICU 式分组需要外部 crate
    println!("{:.2}", 1234.56);
}
输出
1234.56

TypeScript v26.13.1

v26.13.1
// Intl.NumberFormat 与 PHP 的 NumberFormatter 同属 ICU 家族
const us: Intl.NumberFormat = new Intl.NumberFormat('en-US');
const de: Intl.NumberFormat = new Intl.NumberFormat('de-DE');
console.log(us.format(1234.56));
console.log(de.format(1234.56));
输出
1,234.56
1.234,56

语言差异说明

PHP:NumberFormatter(别名 numfmt_* 函数族)按 locale + 样式工作:DECIMAL(普通数字)、CURRENCY/CURRENCY_ACCOUNTING(货币,7.4.1+)、PERCENT、SCIENTIFIC、SPELLOUT/ORDINAL/DURATION(基于规则,依赖 ICU 版本数据)、8.5 新增 DECIMAL_COMPACT_SHORT/LONG、CURRENCY_ISO 等;format() 输出字符串、parse() 逆解析、formatCurrency() 必须带 ISO 货币码(如 USD——格式化器不知汇率,同一数值换货币码即换币别)。精度用 setAttribute(MIN/MAX_FRACTION_DIGITS) 控制(默认最多 2 位小数,手册用户注)。

ICU 版本敏感性分析:en_US DECIMAL 的 1,234.56 是纯数字模式——分组符/小数点由 locale 的 decimalFormat 模式决定,该模式数十年未变(逗号+点号),各 ICU 版本逐字节一致;真正敏感的是 SPELLOUT(拼写规则随 CLDR 增补)、CURRENCY(货币符号 $/US$、负值记账格式、甚至币种名复数在 CLDR 改版时会变)——故货币样式只在此文字讲解:new NumberFormatter('en_US', NumberFormatter::CURRENCY) + formatCurrency(1234.56, 'USD') 典型输出 $1,234.56(负值 accounting 样式为 ($1,234.56)),切勿把符号写死进代码,用 getSymbol(CURRENCY_SYMBOL) 读。

C/C++:标准库无 locale 感知数字格式化;POSIX printf("'%f") 依赖 LC_NUMERIC 且 C locale 无分组符(示例可见原样输出);生产用 ICU 或手写分组。

C#:ToString("N2", culture)——N 定精度分组模式,符号来自 culture;"C" 是货币模式(符号来自 .NET 内置数据,同样建议运行时读)。

Java:NumberFormat.getNumberInstance(culture);getCurrencyInstance + setCurrency(Currency.getInstance("USD"))。

JS/TS:Intl.NumberFormat 同族原生;{style:'currency', currency:'USD'} 对应货币样式。

Python:标准库 format(v, ',.2f') 永远 locale-free;locale.format_string 依赖 OS locale;ICU 级用 Babel 的 format_decimal(v, locale='en_US')。

Go:fmt/strconv 无 locale;golang.org/x/text/message + language.English 的 Printf("%.2f") 才会分组。

Rust:std 格式化无 locale;ICU 级用 icu_decimal/fixed_decimal crate。

对比小结:十门语言里 ICU 系(PHP/JS/Java)与自带数据系(C#)按 locale 出符号,C/C++/Python-std/Go/Rust-std 全靠自己给格式——这正是「全局 locale 已死、参数化当立」在数字格式化上的投影。