有办法让轨道打印出一个数字与逗号在它?

例如,如果我有一个数字540000000 .34,我可以运行<%= number。函数%>,输出“54,000,000.34”

谢谢!


是的,使用NumberHelper。您正在寻找的方法是number_with_delimiter。

 number_with_delimiter(98765432.98, :delimiter => ",", :separator => ".")
 # => 98,765,432.98

您需要使用number_with_delimiter方法。例如:

<%= number_with_delimiter(@number, :delimiter => ',') %>

或者,你也可以使用number_with_precision方法来确保数字总是以小数点后两位的精度显示:

<%= number_with_precision(@number, :precision => 2, :delimiter => ',') %>

对于不使用rails的人:

number.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse

如果您经常这样做,但也仅供参考,因为上面没有暗示它,Rails为number_with_delimiter方法提供了合理的默认值。

#inside controller or view
number_with_delimiter(2444323.4)
#=> 2,444,323.30

#inside console
helper.number_with_delimiter(233423)
#=> 233,423

如果使用最典型的方式,则不需要提供分隔符值。


对于那些不使用rails处理小数的人来说,更好的方法是:

parts = number.to_s.split('.')
parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,")
parts.join('.')

如果需要不同的分隔符,请更改正则表达式中的最后一个','。

额外的,这是正则表达式的工作方式:

gsub replaces everything that matches the regex with the second parameter passed to gsub. In this case that is \\1. \\1 becomes \1 when evaluated which matches the first capture group in the regex. In this regex that is (\d). (\d)(?=(\d\d\d)+) is matching a digit followed by 1 or more groups of 3 digits. The first set of parens is our \1 capture group, the second would be \2. If we were just to leave it at that we would get: 123456.gsub!(/(\d)(?=(\d\d\d)+)/, "\\1,") #=> 1,2,3,456 This is because 1234 matches, 2345 matches and 3456 matches so we put a comma after the 1, the 2, and the 3. By adding the (?!\d) we are matching anything that comes before that doesn't precede a digit so (\d)(?=(\d\d\d)+(?!\d)) means match a digit followed by 3 digits that is not followed by a digit. The reason why this works is that gsub will keep replacing things that match the string. If we were only going to replace the first match then for a number like 123456789 we would get 123456,789. Since 123456,789 still matches our regex we get 123,456,789.

这是我得到代码的地方:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/number_helper.rb#L298-L300

这里是我了解正则表达式中发生了什么的地方:http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm


对于javascript的人

function numberWithDelimiter(value) {
    return (value+"").split("").reverse().join("").replace(/(\d{3})(?=\d)/g, '$1,').split("").reverse().join("")
}

:)


您可以使用来自ActiveSupport的方法

例如:

ActiveSupport::NumberHelper::number_to_currency(10000.1234,{精度:2,单位:"})


另一种不使用helper的解决方案:格式化2位小数,然后替换。由,

puts(("%.2f" % 2.5666).gsub('.',','))
>> 2,57

如果您希望在视图之外添加逗号,并且不想包含某些模块,则可以使用number_to_delimited方法(rails version >= 4.02)。例如:

#inside anywhere
ActiveSupport::NumberHelper.number_to_delimited(1000000) # => "1,000,000"

不管有没有Rails,实现这一点的直接方法是:

require 'active_support'
require 'active_support/core_ext/numeric/conversions'

12345.to_s(:delimited)      # => "12,345"
12345.6789.to_s(:delimited) # => "12,345.6789"

有关更多选项,请参阅活动支持核心扩展-数字-格式。


  def add_commas(numstring)
    correct_idxs = (1..100).to_a.select{|n| n % 6 == 0}.map{|n| n - 1}
     numstring.reverse.chars.join(",").chars.select.with_index{|x, i| i.even? || correct_idxs.include?(i)}.join.reverse
  end

这就是我的红宝石之路

除了编辑: 基本上,它在数字之间添加所有逗号,只选择索引为+ 1% 6的数字

我认为逗号到100是可以的,但如果你想要一个超长的数字,就把100变成一个更高的数字


新语法

number_with_delimiter(@number, delimiter: ",")

如果你想用delimeter赚钱,你可以这么做

number_to_currency(@number)

这也会增加$。如果你使用金钱宝石,那么你可以这样做

Money.new(@number,"USD").format

这也会把$。

number_with_delimiter

ruby的钱

number_to_currency


下面的代码可以同时用于分隔符和精度(API ref)。

ActiveSupport::NumberHelper.number_to_rounded(1234.532, delimiter: ',', precision: 1) 
     

(或者从视图中只使用number_to_rount_round,不需要前缀)

HTH


我在开发Rails 6应用程序时遇到了这个挑战。

如果数字是一个项目的价格或与货币有关,那么你可以使用number_to_currency ActionView Helper

以下是如何做到这一点:

number_to_currency("123456789")                      # => $123456789
number_to_currency(1234567890.50)                    # => $1,234,567,890.50
number_to_currency(1234567890.506)                   # => $1,234,567,890.51
number_to_currency(1234567890.506, precision: 3)     # => $1,234,567,890.506
number_to_currency(1234567890.506, locale: :fr)      # => 1 234 567 890,51 €
number_to_currency(1234567890.50, unit: '₦', delimiter: ',', precision: 0)    # => ₦1,234,567,890
number_to_currency(1234567890.50, unit: "R$", separator: ",", delimiter: "")  # => R$1234567890,50

您可以在Rails文档:number_to_currency中了解更多有关它的信息

这是所有。

我希望这对你们有帮助


对于Ruby的人: 格式化数字(仅限整数),每一组千位之间用逗号分隔。

number = 12345678
numStr1 = number.to_s.reverse.scan(/.{1,3}/).join(',').reverse
puts numStr1             # => 12,345,678

numStr2 = number.to_s.gsub(/\B(?=(...)*\b)/, ',')
puts numStr2             # => 12,345,678

对于Ruby Folks:可以创建函数将逗号设置为大整数。

def number_with_comma(numStr)
   return numStr.to_s.gsub(/\B(?=(...)*\b)/, ',')
end
a = number_with_comma 1234567
puts a   => 1,234,567

x = 9876543
y = number_with_comma x
puts y   => 9,876,543