Rails Number to Indian货币格式转换助手

时间:2011-01-18 20:04:16

标签: ruby-on-rails view

我的号码是100000

当我用户number_to_currency(100000)

它给出 - 100,000.00

但我怎么能得到像1,00,000.00这样的格式

$ 100,000看起来不错,但要用Rs显示它我需要像这样的分隔符(',') - 1,00,00,000.00

6 个答案:

答案 0 :(得分:6)

结帐http://www.funonrails.com/2011/03/number-to-indian-currency-helper-for.html

def number_to_indian_currency(number, html=true)
 txt = html ? content_tag(:span, 'Rs.', :class => :WebRupee) : 'Rs.' 
 "#{txt} #{number.to_s.gsub(/(\d+?)(?=(\d\d)+(\d)(?!\d))(\.\d+)?/, "\\1,")}"
 end 

答案 1 :(得分:1)

我知道这是一个迟到的答案。

但是(RubyMoney)[https://github.com/RubyMoney/money]有一个南亚的格式化选项(也可以用于印度)。

def number_to_indian_rupee(rupees)
  paise = rupees * 100
  Money.new(paise).format(:symbol => 'Rs.', :south_assian_formatting => true)
end

请注意Money.new()  接受货币付款

答案 2 :(得分:1)

def number_to_indian_currency(number)
  if number.present?
    string = number.to_s.split('.')
    number = string[0].gsub(/(\d+)(\d{3})$/){ p = $2;"#{$1.reverse.gsub(/(\d{2})/,'\1,').reverse},#{p}"}
    number = number.gsub(/^,/, '') + '.' + string[1] if string[1] # remove leading comma
    number = number[1..-1] if number[0] == 44
  end
  "Rs.#{number}"
 end

答案 3 :(得分:0)

我建议编写自己的函数来执行此显示格式,而不是覆盖number_to_currency。您可以将其称为r_number_to_currency或类似的东西,并将其放在应用程序视图助手中。

答案 4 :(得分:0)

是潘,我为此写了一个帮手...

像这样 -

def rupees_helper(number)
      number = number.to_s
      rs = ""
      number.reverse.chars.each_with_index{|i, index| rs<<i; rs<<"," if index==2; rs<<"," if (index!=number.chars.count-1&&index>3&&index%2==0) }
      rs = rs.reverse
      return "Rs. " + rs
  end

通过1000000给我“10,00,000卢比”

感谢。

答案 5 :(得分:0)

def number_to_indian_currency(number) 
    if number string = number.to_s.split('.') 
     number = string[0].gsub(/(\d+)(\d{3})$/){ p = $2;"#{$1.reverse.gsub(/(\d{2})/,'\1,').reverse},#{p}"} 
     number = number.gsub(/^,/, '') + '.' + string[1] if string[1] # remove leading comma 
     number = number[1..-1] if number[0] == 44  
    end 
   "Rs.#{number}" 
end

不同组合的样本输出

  helper.number_to_indian_currency(2000)=> “Rs.2,000”

  helper.number_to_indian_currency(2040)=> “Rs.2,040”

  helper.number_to_indian_currency(1222040.54)=> “Rs.12,22,040.54”