如何分割数字格式?

时间:2012-03-13 08:51:18

标签: ruby-on-rails ruby

例如:

12000 = 12 000
5000 = 5 000
1000000 = 1 000 000
20000000 = 20 000 000

没有javascript:)

4 个答案:

答案 0 :(得分:4)

使用number_with_delimiter(12000, :delimiter => ' ')

答案 1 :(得分:4)

Hiya就是你要找的东西:

http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html

like - number_to_currency(1234567890.506,:locale =>:fr)#=> 1 234 567 890,51€

希望这会有所帮助,干杯!

答案 2 :(得分:2)

在问题要求发生变化之前:

number = 12000
number_to_currency(number, :precision => 0, :unit => "", :delimiter =>  number.to_s.length > 4 ? " " : "") # 12 000

number = 5000
number_to_currency(number, :precision => 0, :unit => "", :delimiter =>  number.to_s.length > 4 ? " " : "") # 5000

number = 1000000
number_to_currency(number, :precision => 0, :unit => "", :delimiter =>  number.to_s.length > 4 ? " " : "") # 1 000 000

number = 20000000
number_to_currency(number, :precision => 0, :unit => "", :delimiter =>  number.to_s.length > 4 ? " " : "") # 20 000 000

问题要求发生变化后:

number_to_currency(number, :precision => 0, :unit => "", :delimiter =>  " ")

答案 3 :(得分:2)

纯Ruby:

"20000000".reverse.chars.each_slice(3).map(&:join).join(' ').reverse

所以一定要使用辅助方法。