2024年6月17日 星期一

在 Intl.NumberFormat 控制顯示位數

 在 Intl.NumberFormat 控制顯示位數

前言

  Intl.NumberFormat  提供控制顯示位數的功能,在此把學習的過程做個紀錄。


內容

  範例如下

//
console.log( new Intl.NumberFormat('zh-TW', { 
  style: 'unit', 
  unit : 'liter',
  signDisplay : 'auto',
  minimumIntegerDigits : 3
  }).format( 10) );//010 升

//
console.log( new Intl.NumberFormat('zh-TW', { 
  style: 'unit', 
  unit : 'liter',
  signDisplay : 'auto',
  minimumFractionDigits : 3
  }).format( 10) );//10.000 升

//
console.log( new Intl.NumberFormat('zh-TW', { 
  style: 'unit', 
  unit : 'liter',
  signDisplay : 'auto',
  maximumFractionDigits : 3
  }).format( 10.12345) );//10.123


控制顯示位數的參數分別是 minmumIntegerDigits 、 minmumFractionDigits 與 maxmumFractionDigits , minmumIntegerDigits  是最少整數的顯示位數,如果要顯示的位數彼此數還小前方會補零,minmumFractionDigits  與 maxmumFractionDigits  分別代表最少小數顯示位數與最大小數顯示位數,minmumFractionDigits  碰到為數不足時會補零,maxmumFractionDigits  在超出為數時會直接不顯示。


參考資料

[ developer.mozilla.org ] Intl.NumberFormat


相關文章與資料

使用 Intl.NumberFormat 顯示單位



2024年6月10日 星期一

調整 Intl.NumberFormat 正負號顯示

 調整 Intl.NumberFormat 正負號顯示

前言

  使用  Intl.NumberFormat 時,當數值是負的時候才顯示負號,這是常見的狀況,但如果碰到特殊狀況時可以改嗎?答案是可以的,在此把學習的過程做個紀錄。


內容

  範例如下

let number = 100;
let numberZ = 0;
let numberN = -100;
//
console.log( new Intl.NumberFormat('zh-TW', { 
  style: 'unit', 
  unit : 'liter',
  signDisplay : 'auto',
  }).format( number) );//100 升
//
console.log( new Intl.NumberFormat('zh-TW', { 
  style: 'unit', 
  unit : 'liter',
  signDisplay : 'auto',
  }).format( numberZ) );//0 升
//
console.log( new Intl.NumberFormat('zh-TW', { 
  style: 'unit', 
  unit : 'liter',
  signDisplay : 'auto',
  }).format( numberN) );//-100 升
//
//
console.log( new Intl.NumberFormat('zh-TW', { 
  style: 'unit', 
  unit : 'liter',
  signDisplay : 'always',
  }).format( number) );//+100 升
//
console.log( new Intl.NumberFormat('zh-TW', { 
  style: 'unit', 
  unit : 'liter',
  signDisplay : 'always',
  }).format( numberZ) );//+0 升
//
console.log( new Intl.NumberFormat('zh-TW', { 
  style: 'unit', 
  unit : 'liter',
  signDisplay : 'always',
  }).format( numberN) );//-100 升

//
//
console.log( new Intl.NumberFormat('zh-TW', { 
  style: 'unit', 
  unit : 'liter',
  signDisplay : 'exceptZero',
  }).format( number) );//+100 升
//
console.log( new Intl.NumberFormat('zh-TW', { 
  style: 'unit', 
  unit : 'liter',
  signDisplay : 'exceptZero',
  }).format( numberZ) );//0 升
//
console.log( new Intl.NumberFormat('zh-TW', { 
  style: 'unit', 
  unit : 'liter',
  signDisplay : 'exceptZero',
  }).format( numberN) );//-100 升
//
//
console.log( new Intl.NumberFormat('zh-TW', { 
  style: 'unit', 
  unit : 'liter',
  signDisplay : 'negative',
  }).format( number) );//100 升
//
console.log( new Intl.NumberFormat('zh-TW', { 
  style: 'unit', 
  unit : 'liter',
  signDisplay : 'negative',
  }).format( numberZ) );//0 升
//
console.log( new Intl.NumberFormat('zh-TW', { 
  style: 'unit', 
  unit : 'liter',
  signDisplay : 'negative',
  }).format( numberN) );//-100 升
//
//
console.log( new Intl.NumberFormat('zh-TW', { 
  style: 'unit', 
  unit : 'liter',
  signDisplay : 'never',
  }).format( number) );//100 升
//
console.log( new Intl.NumberFormat('zh-TW', { 
  style: 'unit', 
  unit : 'liter',
  signDisplay : 'never',
  }).format( numberZ) );//0 升
//
console.log( new Intl.NumberFormat('zh-TW', { 
  style: 'unit', 
  unit : 'liter',
  signDisplay : 'never',
  }).format( numberN) );//100 升


透過 signDisplay 可以調整正負號顯示,參數有 auto 、 always 、 exceptZero 、 negative 與 never , auto 就是預設,也是常見的只在負值時顯示負號 ,always 則是無論如何都顯示正負號,包含 0 ,exceptZero 也是都顯示正負號,但 0 的時候不顯示 ,negative 則是指在負值顯示,這行為和 auto 是一樣的,never 則是無論如何都不顯示正負號。範例都是用顯示單位的方式使用,但這個參數不只作用在單位顯示,在百分比與貨幣時一樣適用。


參考資料

[ developer.mozilla.org ] Intl.NumberFormat


相關文章與資料

使用 Intl.NumberFormat 顯示單位

2024年6月3日 星期一

調整 Intl.NumberFormat 顯示單位

 調整 Intl.NumberFormat 顯示單位

前言

  Intl.NumberFormat 可以顯示單位,但有些單位會有需要顯示所縮寫或完整顯示的需求,這時就需要透過參數的調整來顯示,在此把學習的過程做個紀錄。


內容

  範例如下

let number = 100;
//
console.log( new Intl.NumberFormat('zh-TW', { 
  style: 'unit', 
  unit : 'liter',
  unitDisplay : 'short',
  }).format( number) );//100 升
//
console.log( new Intl.NumberFormat('zh-TW', { 
  style: 'unit', 
  unit : 'liter',
  unitDisplay : 'narrow',
  }).format( number) );//100升
//
console.log( new Intl.NumberFormat('zh-TW', { 
  style: 'unit', 
  unit : 'liter',
  unitDisplay : 'long',
  }).format( number) );//100 公升


要調整顯示單位可以透過 unitDisplay 來調整,可以接受的參數有 short 、 narrow 與 long ,範例以公升來示範, short 顯示(空白)與升,narrow 就只顯示升,long 才會顯示(空白)與公升,short 與 narrow 顯示縮寫,差異在於有沒有空白,long 則是完全顯示。


參考資料

[ developer.mozilla.org ] Intl.NumberFormat


相關文章與資料

使用 Intl.NumberFormat 顯示單位