Rails之格式化價格方法

2021-08-31 05:53:38 字數 642 閱讀 3382

一種是直接在試圖中隊**資訊進行格式化

如:<%=sprintf("¥%0.02f",product.price)%>

另外一種是用單獨的輔助方法來處理**格式化,

即number_to_currency,

如:<%=number_to_currency(product.price)%>

顯示結果為$100.00

<%=number_to_currency(product.price,  :unit=>"¥")%>

顯示結果為¥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

日期格式化方法

date型別有一些專門用於將日期格式化為字串的方法 1 todatestring 以特定的於實現的格式顯示星期幾 年月日。2 totimestring 以特定的於實現的格式顯示時分秒和時區。3 tolocaledatestring 以特定於地區的格式顯示星期幾 年月日。4 tolocaltimest...

格式化,強調 數值格式化

聚合函式返回的的數值型別的值有個特點就是沒有被格式化。通過前面的帶 sum 聚合函式的示例,我們可以看到這一點 注意合計值 命令,或者在物件檢視器中選中 displayformat 屬性。在上節的示例中,格式化操作是針對一物件或物件內的表示式,在物件內僅有乙個表示式時它還能正常工作,不過當物件內不有...

js 格式化金額方法

金額千分位加逗號,保留2位小數,不足補零,否則四捨五入 引數說明 num 要格式化的數字 string或者number decimals 保留幾位小數 dec point 小數點符號 thousandssep 千分位符號 return 金額格式的字串,如 1,234,567.45 number nu...