How to format a number 1000 as “1 000”

前端 未结 12 1212
北海茫月
北海茫月 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:08

    This is old but the fastest and most elegant way I could find to do this is:

    def r_delim(s, e)                                                               
      (a = e%1000) > 0 ? r_delim(s, e/1000) : return; s << a                        
    end
    
    r_delim([], 1234567).join(',')
    

    I'll try and add benchmarks at some point.

提交回复
热议问题