Rails 数字到印度货币格式转换助手

发布于 2024-10-12 12:27:09 字数 199 浏览 1 评论 0原文

我有像 100000 这样的数字,

当我用户 number_to_currency(100000) 时

,它给出 - 100,000.00

但我怎样才能获得像 1,00,000.00 这样的格式

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

i have number like 100000

when i user number_to_currency(100000)

it give - 100,000.00

but how can i get format like 1,00,000.00

as $ 100,000 looks good but to show it in Rs i need separators (',') like this - 1,00,00,000.00

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

农村范ル 2024-10-19 12:27:09

结账 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 

checkout 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 
离不开的别离 2024-10-19 12:27:09

RubyMoney 有一个针对南亚的格式化选项(也可用于印度)。

def number_to_indian_rupee(rupees)
  Money.new(rupees, "INR").format(south_asian_number_formatting: true)
end

RubyMoney has a formatting option for south Asia (can be used for India as well).

def number_to_indian_rupee(rupees)
  Money.new(rupees, "INR").format(south_asian_number_formatting: true)
end
-残月青衣踏尘吟 2024-10-19 12:27:09
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
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
帅哥哥的热头脑 2024-10-19 12:27:09

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

I would suggest writing your own function to perform this display formatting, rather than overwriting number_to_currency. You could call it r_number_to_currency or something like that and place it in your application view helper.

香橙ぽ 2024-10-19 12:27:09

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

类似这样的东西 -

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 卢比”

谢谢。

Yes Pan, I wrote a helper for that...

Something like this -

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

which on passing 1000000 gives me "Rs 10,00,000"

Thanks.

缺⑴份安定 2024-10-19 12:27:09
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”
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

Sample Output for different combinations

  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”
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文