package com.echo;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
/**
* Java保留两位小数四种方法
*
* @author Echo
*
*/
public class RetainTwoDecimal {
double retainTwoDecimal = 111231.5585;
public void m1() {
BigDecimal bg = new BigDecimal(retainTwoDecimal);
double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
System.out.println(f1);
}
/**
* DecimalFormat转换最简便
*/
public void m2() {
DecimalFormat df = new DecimalFormat("#.00");
System.out.println(df.format(retainTwoDecimal));
}
/**
* String.format打印最简便
*/
public void m3() {
System.out.println(String.format("%.2f", retainTwoDecimal));
}
public void m4() {
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
System.out.println(nf.format(retainTwoDecimal));
}
public static void main(String[] args) {
RetainTwoDecimal retainTwoDecimal = new RetainTwoDecimal();
retainTwoDecimal.m1();
retainTwoDecimal.m2();
retainTwoDecimal.m3();
retainTwoDecimal.m4();
}
}
转载于:https://www.cnblogs.com/echoing/p/8571891.html