Ste*_*eve 2 ruby haml mongodb mongoid
使用Ruby和Haml,我有一个成本的属性.我相信(我是红宝石的新手),它将是一个Float
目前,下面的行输出我的十进制格式,如4.5而不是4.50,这就是我想要的.
%span.value= "£#{event.beers.first.cost)}"
Run Code Online (Sandbox Code Playgroud)
这是我的啤酒类文件.
class Beer
include Mongoid::Document
embeds_many :ratings
field :name, type: String
field :country, type: Country
field :cost, type: Float
field :photos, type: PhotoArray, default: PhotoArray.new
end
Run Code Online (Sandbox Code Playgroud)
如果您在谈论美国货币,包括:
尝试这个
vals = [123.01, 1234.006, 12, 1234567, 12345678.1,1.001]
vals.map{|num|
num.sprintf('%.2f',num)
.gsub('.00','')
.reverse
.scan(/(\d*\.\d{1,3}|\d{1,3})/)
.join(',')
.reverse
}
Run Code Online (Sandbox Code Playgroud)
在调试器中生成以下内容:
=> ["123.01", "1,234.01", "12", "1,234,567", "12,345,678.10", "1"]
通过编辑连接字符串,可以将其调整为对某些欧洲格式有用,但我对欧洲约定知之甚少。
| 归档时间: |
|
| 查看次数: |
15139 次 |
| 最近记录: |