Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails中货币格式的一种简单方法_Ruby On Rails_Ruby_Currency - Fatal编程技术网

Ruby on rails Rails中货币格式的一种简单方法

Ruby on rails Rails中货币格式的一种简单方法,ruby-on-rails,ruby,currency,Ruby On Rails,Ruby,Currency,今天的大部分时间我都在想如何给我的整型字段设置一种货币格式,结果被无数种不同的方法轰炸了,从使用Money、Rails Money gems到使用自定义助手方法或区域设置 您可以使用number\u to\u currency方法来实现这一点。你说 有以下看法: view.html.erb 您是否尝试过在没有任何选择的情况下使用它? 货币编号(客户付款) 它应该默认为$,在没有任何选项的情况下对我来说运行良好。您可以用几种不同的方法来实现。就我个人而言,我建议在decorator()中执行此操作

今天的大部分时间我都在想如何给我的整型字段设置一种货币格式,结果被无数种不同的方法轰炸了,从使用Money、Rails Money gems到使用自定义助手方法或区域设置

您可以使用
number\u to\u currency
方法来实现这一点。你说 有以下看法:

view.html.erb


您是否尝试过在没有任何选择的情况下使用它?
货币编号(客户付款)

它应该默认为$,在没有任何选项的情况下对我来说运行良好。

您可以用几种不同的方法来实现。就我个人而言,我建议在decorator()中执行此操作,以从视图中删除一些逻辑和格式

如前一个答案中所述,您可以使用
number\u to\u currency
而不使用其他选项来获得所需的正确格式。它可以采用整数、浮点或字符串

number_to_currency 10000
=> "$10,000.00"
另一种方法是使用
money rails
(),如果您想处理
money
对象:

money = Money.new(10000)
=> #<Money fractional:10000 currency:USD>

humanized_money_with_symbol money
=> "$10,000.00"
money=money.new(10000)
=> #
人性化的\u货币\u带有\u符号货币
=> "$10,000.00"
<ul>
    <li><%= number_to_currency(client.payment, :unit => "$", :separator => ".", :delimiter => ",") %></li>
</ul>
:locale - Sets the locale to be used for formatting (defaults to current locale).

:precision - Sets the level of precision (defaults to 2).

:unit - Sets the denomination of the currency (defaults to “$”).

:separator - Sets the separator between the units (defaults to “.”).

:delimiter - Sets the thousands delimiter (defaults to “,”).

:format - Sets the format for non-negative numbers (defaults to “%u%n”). Fields are %u for the currency, and %n for the number.

:negative_format - Sets the format for negative numbers (defaults to prepending a hyphen to the formatted number given by :format). Accepts the same fields than :format, except %n is here the absolute value of the number.

:raise - If true, raises InvalidNumberError when the argument is invalid.
number_to_currency 10000
=> "$10,000.00"
money = Money.new(10000)
=> #<Money fractional:10000 currency:USD>

humanized_money_with_symbol money
=> "$10,000.00"