Intl.NumberFormat
åºçº¿
广æ³å¯ç¨
èª 2017å¹´9æ èµ·ï¼æ¤ç¹æ§å·²å¨ä¸»æµæµè§å¨ä¸å¾å°æ¯æï¼å¯å¨å¤§å¤æ°è®¾å¤åæµè§å¨çæ¬ä¸æ£å¸¸ä½¿ç¨ã
Intl.NumberFormat 对象è½ä½¿æ°åå¨ç¹å®çè¯è¨ç¯å¢ä¸æ ¼å¼åã
å°è¯ä¸ä¸
const number = 123456.789;
console.log(
new Intl.NumberFormat("de-DE", { style: "currency", currency: "EUR" }).format(
number,
),
);
// Expected output: "123.456,79 â¬"
// The Japanese yen doesn't use a minor unit
console.log(
new Intl.NumberFormat("ja-JP", { style: "currency", currency: "JPY" }).format(
number,
),
);
// Expected output: "ï¿¥123,457"
// Limit to three significant digits
console.log(
new Intl.NumberFormat("en-IN", { maximumSignificantDigits: 3 }).format(
number,
),
);
// Expected output: "1,23,000"
æé 彿°
Intl.NumberFormat()-
å建ä¸ä¸ªæ°ç
NumberFormat对象ã
éææ¹æ³
Intl.NumberFormat.supportedLocalesOf()-
è¿åä¸ä¸ªæ°ç»ï¼å ¶å å«çé£äºè¢«æä¾ç忝æçåºåï¼localeï¼ï¼èè¿è¡æ¶çé»è®¤çåºåä¸ä¼åºç°å¨è¯¥æ°ç»ã
å®ä¾æ¹æ³
Intl.NumberFormat.prototype.format()-
getter 彿°ï¼æ ¹æ®æ¤
Intl.NumberFormat对象çåºåè®¾ç½®åæ ¼å¼åéé¡¹æ ¼å¼åæ°åã Intl.NumberFormat.prototype.formatToParts()-
è¿åä¸ä¸ªå¯¹è±¡æ°ç»ï¼å ¶ä¸è¡¨ç¤ºçæ¯æ°åå符串çå个é¨åï¼å¯ä»¥ç¨äºèªå®ä¹æ¬å°åæ ¼å¼ã
Intl.NumberFormat.prototype.formatRange()-
getter 彿°ï¼æ ¹æ®è°ç¨è¯¥æ¹æ³ç
Intl.NumberFormat对象çåºåè®¾ç½®åæ ¼å¼åéé¡¹æ ¼å¼åä¸ä¸ªæ°åçèå´ã Intl.NumberFormat.prototype.formatRangeToParts()-
è¿åä¸ä¸ªå¯¹è±¡æ°ç»ï¼å ¶ä¸è¡¨ç¤ºçæ¯æ°åèå´çå符串çå个é¨åï¼å¯ä»¥ç¨äºèªå®ä¹æ¬å°åæ ¼å¼ã
Intl.NumberFormat.prototype.resolvedOptions()-
è¿åä¸ä¸ªæ°å¯¹è±¡ï¼å ¶å±æ§è½å¤åæ ç¸åºå¯¹è±¡å¨åå§åæ¶è®¡ç®æå¾çåºååæ ¼å¼åé项ã
示ä¾
>åºç¡ç¨ä¾
å¨ä¸æå®åºå设置çåºæ¬ç¨ä¾ä¸ï¼è¿åé»è®¤åºååé»è®¤é项ä¸çæ ¼å¼åå符串ã
const number = 3500;
console.log(new Intl.NumberFormat().format(number));
// 妿å¨ç¾å¼è±è¯åºå â '3,500'
ä½¿ç¨ locales
æ¤ç¤ºä¾å±ç¤ºäºæ¬å°æ°åæ ¼å¼åçä¸äºååã为äºå¾å°ç¨æ·åºç¨æ¥å£ä½¿ç¨çè¯è¨æ ¼å¼ï¼è¯·ç¡®ä¿ä½¿ç¨ locales åæ°æå®è¯¥è¯è¨ï¼å¯è½è¿æä¸äºå¤éè¯è¨ï¼ï¼
const number = 123456.789;
// å¾·è¯ä½¿ç¨éå·ï¼,ï¼ä½ä¸ºå°æ°ç¹ï¼ä½¿ç¨å¥å·ï¼.ï¼ä½ä¸ºåä½åé符
console.log(new Intl.NumberFormat("de-DE").format(number));
// â 123.456,789
// 大夿°é¿æä¼¯è¯å½å®¶ä½¿ç¨é¿æä¼¯è¯æ°å
console.log(new Intl.NumberFormat("ar-EG").format(number));
// â ١٢٣٤٥٦٫٧٨٩
// India uses thousands/lakh/crore separators
console.log(new Intl.NumberFormat("en-IN").format(number));
// â 1,23,456.789
// éè¿ç¼å·ç³»ç»ä¸ç nu æ©å±é®è¯·æ±ï¼ä¾å¦ï¼ä¸æåè¿å¶æ°å
console.log(new Intl.NumberFormat("zh-Hans-CN-u-nu-hanidec").format(number));
// â ä¸äºä¸,åäºå
.ä¸å
«ä¹
//å½è¯·æ±çè¯è¨ä¸è¢«æ¯æï¼ä¾å¦å·´éï¼å
å«ä¸ä¸ªåæ»è¯è¨å°å°¼ï¼è¿æ¶åå°±ä¼ä½¿ç¨å°å°¼è¯
console.log(new Intl.NumberFormat(["ban", "id"]).format(number));
// â 123.456,789
ä½¿ç¨ options
å¯ä»¥ä½¿ç¨ options åæ°èªå®ä¹ç»æï¼
const number = 123456.789;
// è¦æ±è´§å¸æ ¼å¼
console.log(
new Intl.NumberFormat("de-DE", { style: "currency", currency: "EUR" }).format(
number,
),
);
// 123.456,79 â¬
// æ¥å
ä¸ä½¿ç¨å°æ°ä½
console.log(
new Intl.NumberFormat("ja-JP", { style: "currency", currency: "JPY" }).format(
number,
),
);
// ï¿¥123,457
// éå¶ä¸ä½æææ°å
console.log(
new Intl.NumberFormat("en-IN", { maximumSignificantDigits: 3 }).format(
number,
),
);
// 1,23,000
// 带æåä½çæ ¼å¼å
console.log(
new Intl.NumberFormat("pt-PT", {
style: "unit",
unit: "kilometer-per-hour",
}).format(50),
);
// 50 km/h
console.log(
(16).toLocaleString("en-GB", {
style: "unit",
unit: "liter",
unitDisplay: "long",
}),
);
// 16 litres
æå
³è¯¦å°½çé项å表ï¼åè§ Intl.NumberFormat() æé 彿°é¡µé¢ã
è§è
| è§è |
|---|
| ECMAScript® 2027 Internationalization API Specification> # numberformat-objects> |