2024年5月20日 星期一

使用 Intl.NumberFormat 選擇記帳格式

 使用 Intl.NumberFormat 選擇記帳格式

前言

  在查閱 Intl.NumberFormat 的功能時發現有個參數可以調整記帳格式,在此把學習的過程做個紀錄。


內容

  範例如下

let number = -1234;
//
console.log( new Intl.NumberFormat('zh-TW', { 
  style: 'currency', 
  currency: 'TWD' ,
  currencySign : 'standard'
  
  }).format( number) );//-$1,234.00

//
console.log( new Intl.NumberFormat('zh-TW', { 
  style: 'currency', 
  currency: 'TWD' ,
  currencySign : 'accounting'
  
  }).format( number) );//($1,234.00)


調整記帳格式的差異在於有的區域記帳時顯示負值的方式不是用負號,而是使用括號,可以透過 currencySign 來調整,參數內容可以是 stardard 與 accounting ,預設為 stardard  ,範例使用 stardard 如預期在前方加負號顯示, 使用 accounting  則是越括號來表達。


參考資料

[ developer.mozilla.org ] Intl.NumberFormat


相關文章與資料

初探 Intl.NumberFormat

使用 Intl.NumberFormat 選擇貨幣顯示的方式


沒有留言:

張貼留言