How to format a number 1000 as “1 000”

前端 未结 12 1229
北海茫月
北海茫月 2020-12-14 06:19

I need a way to format numbers. I stored some numbers in my DB table, e.g. 12500, and would like to print them in this format 12 500 (so there is a

12条回答
  •  心在旅途
    2020-12-14 07:12

    I just stumbled on this thread while looking for a way to format a value as US currency. I took a slightly different approach to the regex solutions proposed:

    amt = 1234567890.12
    f_amt = format("$%.2f",amt)
    i = f_amt.index(".")
    while i > 4
      f_amt[i-3]=","+f_amt[i-3]
      i = f_amt.index(",")
    end
    
    f_amt
    => "$1,234,567,890.12"
    

    This could be parameterized for formatting other currencies.

提交回复
热议问题