2024年4月29日 星期一

初探 Intl.NumberFormat

 初探 Intl.NumberFormat

前言

  這次來學習透過 Intl.NumberFormat 顯示貨幣價值,在此做個紀錄。


內容

  範例如下

let number = 1234;
//
console.log( new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' 
  }).format( number) );//1.234,00 €
//
console.log( new Intl.NumberFormat('ja-JP', { style: 'currency', currency: 'JPY' 
  }).format( number) );//¥1,234
//
console.log( new Intl.NumberFormat('zh-TW', { style: 'currency', currency: 'TWD' 
  }).format( number) );//$1,234.00


使用的方法跟其他的 Intl 差不多,不過要注意 currency 這個參數,這個參數引響顯示幣值的符號,要輸入的代碼可以在 [ en.wikipedia.org ] ISO 4217 查詢,區域碼會引響的是貨幣符號在前後與是否顯示小術後兩位。


參考資料

[ developer.mozilla.org ] Intl.NumberFormat

[ en.wikipedia.org ] ISO 4217

沒有留言:

張貼留言