Skip to content

Instantly share code, notes, and snippets.

@yano3nora
Last active March 19, 2022 13:51
Show Gist options
  • Save yano3nora/a6bb1bda94be7792d02f3f4ca8037fdd to your computer and use it in GitHub Desktop.
Save yano3nora/a6bb1bda94be7792d02f3f4ca8037fdd to your computer and use it in GitHub Desktop.
[js: Intl.NumberFormat] Number Format built-in API of Browser JS and Node.js #js

Intl.NumberFormat - developer.mozilla.org
JavaScriptで数値フォーマットする標準API「Intl.NumberFormat」 (カンマ区切り、円・ドル表記、漢数字など)

// 3 桁カンマ区切り
const formatter = new Intl.NumberFormat('ja-JP')
formatter.format(1000) // '1,000'

// ついでに浮動小数の精度まるめ (四捨五入)
// maximumFractionDigits option で少数点の最大桁数を指定可能
//
formatter.format(123.5554) // '123.555'
formatter.format(123.5555) // '123.556'

// 金額フォーマット
const formatter = new Intl.NumberFormat('ja-JP', {
  style: 'currency',
  currency: 'JPY',
})
formatter.format(1000) // '¥1,000'

// 漢数字 (中国で利用される簡体字の漢数字 10 進数)
const formatter = new Intl.NumberFormat('zh-Hans-CN-u-nu-hanidec')
formatter.format(1234567890) // '一,二三四,五六七,八九〇'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment