中关村村草 发表于 2011-02-25 10:16

Rails之格式化价格方法

转:lonelystarxing


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
页: [1]
查看完整版本: Rails之格式化价格方法