LoginSignup
1
1

More than 5 years have passed since last update.

CoffeeScriptで日本語の桁区切り (例 123456789 -> 1億2345万6789)

Posted at

CoffeeScriptで数字を
1億2345万6789
などと日本語の桁区切りで表示する関数 Beautify.jpn です。

beautify.coffee
class Beautify
    @jpn: (val) ->
        unit = ['', '万', '億', '兆', '京', '垓']
        num_arr = String(val).replace(/(\d)(?=(\d\d\d\d)+$)/g, "$1,").split(",").reverse()
        num_arr2 = (
            num.replace(/^[0]+/g, '') + (if num is "0000" then "" else unit[i]) for num, i in num_arr
            )
        return (
            num_arr2.reverse().toString().replace(/,/g, '')
            )

console.log Beautify.jpn(123456789) # 1億2345万6789
console.log Beautify.jpn(120000789) # 1億6789
1
1
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
1
1