当前位置:诺佳网 > 电子/半导体 > 区块链 >

只保留两位小数,多余的删掉

时间:2018-01-21 | 栏目:区块链 | 点击:

public class Test { public static void main(String[] args) { double d = 756.2345566; //方法一:最简便的方法,调用DecimalFormat类 DecimalFormat df = new DecimalFormat(".00"); System.out.println(df.format(d)); //方法二:直接通过String类的format函数实现 System.out.println(String.format("%.2f", d)); //方法三:通过BigDecimal类实现 BigDecimal bg = new BigDecimal(d); double d3 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); System.out.println(d3); //方法四:通过NumberFormat类实现 NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMaximumFractionDigits(2); System.out.println(nf.format(d)); } }

您可能感兴趣的文章:

相关文章