LoginSignup
14
10

More than 3 years have passed since last update.

jsで数値にカンマをつける

Last updated at Posted at 2019-02-05

正規表現を使う

const NumberWithDelimiter = (number) => {
  return String(number).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1,');
}

number.toLocaleString()を使う


var num = 100000;
num.toLocaleString();
// -> "100,000"
var num = "100000";
num.toLocaleString();
// -> "100000"
var num = Number("100000")
num.toLocaleString();
// -> "100,000"

ブラウザ実装状況
https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString#Browser_compatibility

そこそこ実装されているので
number.toLocaleStringで問題なさそう

14
10
1

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
14
10