如何将数字1000格式化为“1 000” [英] How to format a number 1000 as "1 000"
本文介绍了如何将数字1000格式化为“1 000”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
12500
,并希望以这种格式打印 12 500
(所以每3位有一个空格)。有没有一个优雅的方式来做到这一点? http://www.justskins.com/forums/format-number-with-comma-37369.html
$ b $ (除非你使用Rails,ActiveSupport有办法做到这一点),但你可以使用正则表达式如$ / pre> formatted_n = n.to_s.reverse.gsub(/...(?=.)/,'\&,')。reverse
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 space every 3 digits). Is there an elegant way to do this?
解决方案
see: http://www.justskins.com/forums/format-number-with-comma-37369.html
there is no built in way to it ( unless you using Rails, ActiveSupport Does have methods to do this) but you can use a Regex like
formatted_n = n.to_s.reverse.gsub(/...(?=.)/,'\&,').reverse
这篇关于如何将数字1000格式化为“1 000”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文