Java 保留小数两位



一、用format
String.format("%.2f",f)

二、用数学方式:
(float) (Math.round(value.getT_EXT4() * 100)) / 100



String.foramt中格式相关:
代码如下:
1 System.out.println(String.format("%01.2f", 0.3f));
2 System.out.println(String.format("%02.2f", 0.3f));
3 System.out.println(String.format("%03.2f", 0.3f));
4 System.out.println(String.format("%04.2f", 0.3f));
5 System.out.println(String.format("%05.2f", 0.3f));
6 System.out.println(String.format("%06.2f", 0.3f));

输出:
1 0.30
2 0.30
3 0.30
4 0.30
5 00.30
6 000.30

 





免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM