如何将数字 1000 格式化为“1 000"? [英] How to format a number 1000 as "1 000"

查看:14
本文介绍了如何将数字 1000 格式化为“1 000"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种格式化数字的方法.我在我的数据库表中存储了一些数字,例如12500,并希望以这种格式 12 500 打印它们(因此每 3 位有一个空格).有没有优雅的方法来做到这一点?

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?

推荐答案

参见:http://www.justskins.com/forums/format-number-with-comma-37369.html

没有内置的方法(除非你使用 Rails,ActiveSupport 确实有这样做的方法),但你可以使用像

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆