因为平时很少练习所以考试两个小时答十道题对于我来讲还是很费劲并且平时没有很注意关于小数点的问题平时几乎都是用printf来写并没有挖掘好用的写法今天来举例好用的方法1.String.formatdouble money 123.456;// 保留两位小数String info 您本次消费金额 String.format(%.2f, money) 元;System.out.println(info);// 输出您本次消费金额123.46 元如果要四舍五入就用上边这个方法2.DecimalFormat 单独格式化数字适合多次复用统一格式import java.text.DecimalFormat;public class Test {public static void main(String[] args) {DecimalFormat df new DecimalFormat(0.00);double score 89.128;String text 期末总分为 df.format(score) 分;System.out.println(text);}}今日分享结束下次见。