LoginSignup
0
0

More than 5 years have passed since last update.

【Javascript】JSで数字を「,」区切りで表示

Last updated at Posted at 2018-10-31

数字を「,」区切りで表示するJS

    // カンマ区切りに変換
    function number_separated(num)
    {
        // 整数と小数に分解
        var intNum = Math.floor(num);

        // カンマ区切りに文字を置換
        var re = ("" + intNum).replace( /(\d)(?=(\d{3})+\b)/g, "$1," );

        // 小数の場合
        if (intNum !== num) {
            // 小数を抜き出し
            var decNum = ("" + num).split(".")[1];
            re = re + "." + decNum;
        }

        return re;
    }
0
0
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
0
0