/**
* 保留两位小数
*/
@Test
public void test2(){
BigDecimal bd=null;
// 始终显示两位小数
double[] num = {3.4567, 3, 0.4567, 2343.333, 133, 3.1415};
for (double n : num) {
bd = new BigDecimal(n);
bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
System.out.println("结果"+bd);
}
}
@Test
public void main (){
System.out.println(String.format("我的名字叫%s","小明")); // 打印:我的名字叫小明
System.out.println(String.format("%.2f", 12.1212312312312312313253456453)); // 打印:12.12
System.out.println(String.format("%f", 12.123)); // 打印:12.1230006
Float string = 12.132342342786478264782367589535F;
System.out.println(String.format("%.2f",string));
}
}
结果如下


发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/190213.html原文链接:https://javaforall.net
