Rails之格式化价格方法


一种是直接在试图中队价格信息进行格式化
如:
<div class="price">
<%=sprintf("¥%0.02f",product.price)%>
</div>

另外一种是用单独的辅助方法来处理价格格式化,
即number_to_currency,
如:
<div class="price">
    <%=number_to_currency(product.price)%>
</div>
显示结果为$100.00
<div class="price">
    <%=number_to_currency(product.price,  :unit=>"¥")%>
</div>
显示结果为¥100.00

完整描述为number_to_currency(number,options={}),
将数字格式化为金额字符串,
options是一个hash,用于定制输出格式
包含:precesion    指定数字的精度,默认为2
    :unit         指定货币输出格式,默认为$
    :separator    指定整数与小数之间的分隔符,默认为 "."
    :delimiter    指定整数部分的定界符
示例:
number_to_currency(1234567890.123,:unit=>"¥",:separator=>",",:delimiter=>".")
->1.234.567.890,123

转载于:https://www.cnblogs.com/lonelystarxing/archive/2011/02/24/1963717.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值