Rails number_to_currency删除十进制右边的尾随零

cti*_*y79 8 ruby ruby-on-rails number-to-currency

我有一个动态生成的表,乘以价格*数量.部分价格为部分美分.例如

如果一个项目的价格是0.0375我可以在我的表中显示为

number_to_currency(0.0375,:precision => 4)
=> $0.0375
Run Code Online (Sandbox Code Playgroud)

但是对于价格是标准的2位十进制数的数量,我得到

number_to_currency(33.95,:precision => 4)
  => $39.9500
Run Code Online (Sandbox Code Playgroud)

我需要一种方法来修剪十进制值的尾随零.请记住,输出在Model.each块中,所以我不确定我是否可以有条件地修改precision参数.

小智 19

尝试指定strip_insignificant_zeros选项:

number_to_currency(33.95, precision: 4, strip_insignificant_zeros: true)
Run Code Online (Sandbox Code Playgroud)

它应该在小数分隔符后删除零.以下是此选项的说明.