1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

To convert an integer into a comma-separated string

Last updated at Posted at 2015-09-04

何ぞ JavaScript には 整數を comma くぎりにする 方法 見つからず。
ちなみに Number.prototype.toLocaleString() は comma くぎりとする 保證は 有らず。
これは 正規表現 (regular expression) により おこなへば よろしからむ。

addCommas.js
s = String(number - 0).replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');

みそは 正規表現 x(?=y) か。 これ matches x only if x is followed by y。 所謂 "肯定的 先讀み" なり。
ちなみに 否定的 先讀みは x(?!y), 肯定的 後讀みは (?<=y)x, 否定的 後讀みは (?<!y)x と なる。
たゞし、JavaScript にては 後讀みは 無き やうなり。

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?