如果數據庫沒有自己的精度,則可以使用自己的說明符,例如,使用System.out.println("%.2f",<money variable>);
。其中%.2f
使用.
和f
後以限制限制至2位是浮動的數據類型
在[這個網站]的說明符[1]
[1]:http://www.drdobbs.com/jvm/184405653我引用以下內容:
`The toDouble() method converts a monetary value to a double floating-point value.
However, you should avoid using the converted value for computational purposes, owing
to the problems associated with the use of floating-point data types with
monetary data.
You can also convert a monetary value to a long integer data type (having an implied
scaling factor of 2). In this case, an automatic round-off to the nearest whole cent is
performed.`
所以你的錢變量轉換爲任何類型的足夠的範圍,你可以使用轉換後的值,並投了小數2或任意數量的你喜歡的地方。
你是什麼意思的輸出 - 你的意思是打印的價值? – BeRecursive 2012-04-26 11:44:45
你有什麼嘗試?你看過http://stackoverflow.com/questions/2379221/java-currency-number-format? – radimpe 2012-04-26 11:45:38
Java標準庫沒有定義'Money'類,所以請說明您是使用定製的Money類,還是有興趣使用'double'值。 – posdef 2012-04-26 11:45:50