Java保留小数点后两位

public static void main(String[] args) {
//使用#.##格式时,如果小数点后小于两位,则会原样输出,大于两位时则保留两位小数;使用#.00时,不管小数点后几位,都会保留两位小数。
DecimalFormat df = new DecimalFormat("#.##");
System.out.println(df.format(1.5));
System.out.println(new DecimalFormat("#.00").format(1));

}


猜你喜欢

转载自blog.csdn.net/qq_25816185/article/details/80647389