4
2

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.

JS 数値を カンマ区切り かつ 小数点以下をゼロ埋め して表示する

Last updated at Posted at 2019-06-03

Javascript で数値を カンマ区切り かつ 小数点以下をゼロ埋め して表示する方法につまづいたので備忘録で書いておきます
(今回は小数第4位で切り捨てする形で書きます)

やりたかったこと

12345 → "12,345.000"
12345.67 → "12,345.670"
12345.6789 → "12,345.678"

最初に書いてたコード

const digit = 3;
const number = 12345.67;
number.toFixed(digit).toLocaleString();
// "12345.670"

と、カンマが表示されませんでした。

解決策

toLocaleString が有能だった

const digit = 3;
const number = 12345.67;
number.toLocaleString(undefined, {
    minimumFractionDigits: digit,
    maximumFractionDigits: digit
});
4
2
0

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
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?