DecimalFormat格式化输出带小数的数字类型
生活随笔
收集整理的這篇文章主要介紹了
DecimalFormat格式化输出带小数的数字类型
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
剛開始
網上找到了DecimalFormat對象
double d = 333333333.333333333; DecimalFormat decimalFormat = new DecimalFormat("#.#"); System.out.println(decimalFormat.format(d));結果為333333333.3,發現假設要輸出后面的小數,就須要把字符串改為"#.####",沒添加一個#就添加一位小數我認為這個非常麻煩,看了下幫助文檔,發現有Max值。試了下輸出
DecimalFormat decimalFormat = new DecimalFormat("#.#"); System.out.println("Integer:"+decimalFormat.getMaximumIntegerDigits()+"\nFraction:"+decimalFormat.getMaximumFractionDigits());結果為
Integer:2147483647
Fraction:0
看來是我版本號默認的小數位數值。太少了,所以改動下值
double d = 333333333.333333333; DecimalFormat decimalFormat = new DecimalFormat("#.#"); //decimalFormat.setMaximumIntegerDigits(100);//顯示整數的最大字數,默認值足夠大 decimalFormat.setMaximumFractionDigits(1000);//顯示小時位的最大字數 System.out.println("Integer:"+decimalFormat.getMaximumIntegerDigits()+"\nFraction:"+decimalFormat.getMaximumFractionDigits()); System.out.println(decimalFormat.format(d));輸出結果為 Integer:2147483647
Fraction:1000
333333333.3333333
最終符合要求了
轉載于:https://www.cnblogs.com/blfbuaa/p/7101262.html
總結
以上是生活随笔為你收集整理的DecimalFormat格式化输出带小数的数字类型的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 跨域,json与jsonp格式
- 下一篇: 理解__repr__