【问题标题】:How to separate a number according to the Indian numbering system in Rails?如何根据 Rails 中的印度编号系统分隔数字?
【发布时间】:2016-09-25 11:44:46
【问题描述】:

我使用number_with_delimiter 方法在Ruby on Rails 的发票中为数字添加逗号。但是数字格式导致23,324,455,而不是2,33,24,455,即Indian Rupees format

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

我必须生成金额为卢比的发票,因此格式应为xx,xx,xxx.00。在 Rails 中可以吗?如何实现?

这可以用 JavaScript 完成,但问题是,我使用 PDFKit gem 生成了 PDF 格式的发票,它不响应 JavaScript。我在加载文档时使用了所需的 js 代码。

$(document).ready(function(){
  $('.totalvalue').each(function(){
    value = $('.totalvalue').text();
    $('.totalvalue').html(
      (value+"").split("").reverse().join("").replace(/(\d{3})(?=\d)/g,        '$1,').split("").reverse().join("")
    )
  })
})

【问题讨论】:

  • 感谢@stefan 编辑问题。

标签: javascript ruby-on-rails ruby pdfkit number-with-delimiter


【解决方案1】:

您可以使用money gem,并专门探索它的formatting options

Money.new(10000000, "INR").format(:symbol => false, 
                                  :south_asian_number_formatting => true)    
#=> "1,00,000.00"

【讨论】:

  • 这工作正常,但正如你所提到的,当金额为 10000000 时,它给出 1,00,000.00 但我期望的是 1,00,00,000.00,而且当它有像 123456.5 这样的小数点时,它给出 1,234.56而不是 1,23,456.50
  • @Nandy 该宝石适用于派斯 - 因此该值应以派斯而不是卢比为单位指定。根据文档,Represents monetary values as integers, in cents. This avoids floating point rounding errors.
【解决方案2】:

不优雅,但有效:

("%.2f" % 23324455).reverse.gsub(/(?<=.{6})(\d\d?)/, ',\1').reverse
# => "2,33,24,455.00"

【讨论】:

  • 你能补充一些解释吗?目前尚不清楚这是如何工作的。
猜你喜欢
  • 2014-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多