使用 Intl.NumberFormat 選擇貨幣顯示的方式
前言
在先前的 初探 Intl.NumberFormat 介紹了顯示貨幣的方式,但貨幣都是用符號來顯示, Intl.NumberFormat 還支援其它的顯示方式,在此把學習的過程做個紀錄。
內容
範例如下
let number = 1234;
//
console.log( new Intl.NumberFormat('de-DE', {
style: 'currency',
currency: 'EUR',
currencyDisplay : 'code'
}).format( number) );//1.234,00 EUR
//
console.log( new Intl.NumberFormat('ja-JP', {
style: 'currency',
currency: 'JPY',
currencyDisplay : 'symbol'
}).format( number) );//¥1,234
//
console.log( new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'TWD' ,
}).format( number) );//NT$1,234.00
//
console.log( new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'TWD' ,
currencyDisplay : 'narrowSymbol'
}).format( number) );//$1,234.00
//
console.log( new Intl.NumberFormat('zh-TW', {
style: 'currency',
currency: 'TWD' ,
currencyDisplay : 'name'
}).format( number) );//1,234.00 新台幣
控制顯示貨幣的方式是透過 currencyDisplay 來調整,參數可輸入 code 、 symbol 、 narrowSymbol 與 name , code 顯示就是用貨幣碼來顯示, symbol 就是符號也是預設的參數 , narrowSymbol 目前只發現在顯示非本國貨幣時會有差異,範例示範在 en-US 顯示台幣時的差異,最後,name 是用文字來顯示貨幣。
參考資料
[ developer.mozilla.org ] Intl.NumberFormat
沒有留言:
張貼留言