LoginSignup
1
0

More than 3 years have passed since last update.

整数に3桁区切りカンマを入れる (JavaScript)

Last updated at Posted at 2020-10-14

結果

処理前

123456789

処理後

123,456,789

方法

方法1

toLocaleStringを使う
https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString

方法2

NumberFormatを使う
https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat

方法3

humanize を使う
https://github.com/HubSpot/humanize

実装

実装方法2

var number = 3500;

console.log(new Intl.NumberFormat().format(number));
// → '3,500' 英語(U.S.)ロケールの場合

実装方法3

準備

yarn add humanize-plus

実装

import Humanize from 'humanize-plus'

let r = Humanize.intComma(123456789)
console.log(r) // 123,456,789
1
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0