转载请注明,原文地址:
<code>http://www
.lgygg.wang/lgyblog/2020
/02
/19
/java%e5%9b%9b%e8%88%8d%e4%ba%94%e5%85%a5/ /<code>
1.方法一 String.format和DecimalFormat
<code>String tmp = String.format("%.2f"
, x); String tmp2 = String.format("%.2f"
, y); System.out
.println(tmp+" "
+tmp2);Double
d =Double
.valueOf(tmp); System.out
.println("方法一 Double.valueOf(tmp):"
+d);Double
d2 =Double
.parseDouble(tmp); System.out
.println("方法一 Double.parseDouble(tmp):"
+d2); DecimalFormat df = new DecimalFormat("#.##"
); System.out
.println("方法一 df.format(x):"
+df.format(x)); System.out
.println("方法一 df.format(z):"
+df.format(z)); DecimalFormat df2 = new DecimalFormat("#0.00"
); System.out
.println("方法一 df2.format(x):"
+df2.format(x)); System.out
.println("方法一 df2.format(z):"
+df2.format(z)); /<code>
总结:(1)可以使用String的format()来保存小数点后几位,但是String.format()方法返回的结果是一个String,所以如果你想得到的是一个double,那么 你还需要使用Double.valueOf或Double.parseDouble来转换(2)额外说一下Double.valueOf和Double.parseDouble的区别,它们的区别就是返回值的类型不一样,Double.valueOf返回的是Double(对象类型),而Double.parseDouble返回的是double(基础类型)
(3)还可以通过DecimalFormat来进行四舍五入,但它其实和String.format的作用差不多,只不过format的格式不大一样
String.format(“%.2f”, x)会保留两位小数,最后一位如果是0也会显示
DecimalFormat要使用new DecimalFormat(“0.00”)保留两位小数,最后一位如果是0也会显示
2.方法二 Math.round
<code>System.out
.println("方法二 (Math.round(x)*100)/100:"
+(Math.round(x*100
))/100.0f
); System.out
.println("方法二 (Math.round(y)*100)/100:"
+(Math.round(y*100
))/100.0f
); System.out
.println("方法二 (Math.round(z)*100)/100:"
+(Math.round(z*100
))/100.0f
); /<code>
总结:其实说到四舍五入我第一个想到的就是Math.round()方法,但是Math.round()返回的结果是一个整数,没有设置保留小数点后几位的方法,但是也可以先将它小数的后两位变成整数,再除以100.0f(不能除以100,否则返回的是整数)即可,但是如果希望结果是3.20,那么还是需要使用format来进行格式化。总之Math.round()进行四舍五入比较麻烦,不推荐使用。
3.方法三 BigDecimal
以下是来自百度百科对BigDecimal的介绍。Java在java.math包中提供的API类BigDecimal,用来对超过16位有效位的数进行精确的运算。双精度浮点型变量double可以处理16位有效数。在实际应用中,需要对更大或者更小的数进行运算和处理。
float和double只能用来做科学计算或者是工程计算,在商业计算中要用java.math.BigDecimal。
BigDecimal所创建的是对象,我们不能使用传统的+、-、*、/等算术运算符直接对其对象进行数学运算,而必须调用其相对应的方法。
方法中的参数也必须是BigDecimal的对象。构造器是类的特殊方法,专门用来创建对象,特别是带有参数的对象。
<code> BigDecimal bd =new
BigDecimal(x); BigDecimal bs = bd.setScale(2
, BigDecimal.ROUND_HALF_UP); System.out
.println("方法三 bs:"
+bs); BigDecimal bd2 =new
BigDecimal(y); BigDecimal bs2 = bd2.setScale(2
, BigDecimal.ROUND_HALF_UP); System.out
.println("方法三 bs2:"
+bs2); BigDecimal bd3 =new
BigDecimal(z); BigDecimal bs3 = bd3.setScale(2
, BigDecimal.ROUND_HALF_UP); System.out
.println("方法三 bs3:"
+bs3); /<code>
总结:BigDecimal很方便进行四舍五入,但是由于它是用来对超过16位有效位的数进行精确的运算,也意味着占用的内存比double多
4.方法四 NumberFormat
<code>NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMaximumFractionDigits(2
); nf.setRoundingMode(RoundingMode.HALF_UP); System.out
.println("方法三 nf.format(x):"
+nf.format(x)); System.out
.println("方法三 nf.format(y):"
+nf.format(y)); System.out
.println("方法三 nf.format(z):"
+nf.format(z)); /<code>
总结:NumberFormat也比较方便,但是返回的也是StringNumberFormat 是所有数值格式的抽象基类。此类提供格式化和解析数值的接口。
5.源码
<code>package com.lgy.test; import java.math.BigDecimal; import java.math.RoundingMode; import java.text.DecimalFormat; import java.text.NumberFormat;public
class
Test
{public
static
void
main
(String[] args
) {float
x =3.1984f
;double
y =3.142840395056
;float
z =0.1984f
; String tmp = String.format("%.2f"
, x); String tmp2 = String.format("%.2f"
, y); System.out
.println(tmp+" "
+tmp2); Double d = Double.valueOf(tmp); System.out
.println("方法一 Double.valueOf(tmp):"
+d); Double d2 = Double.parseDouble(tmp); System.out
.println("方法一 Double.parseDouble(tmp):"
+d2); DecimalFormat df =new
DecimalFormat("#.##"
); System.out
.println("方法一 df.format(x):"
+df.format(x)); System.out
.println("方法一 df.format(z):"
+df.format(z)); DecimalFormat df2 =new
DecimalFormat("#0.00"
); System.out
.println("方法一 df2.format(x):"
+df2.format(x)); System.out
.println("方法一 df2.format(z):"
+df2.format(z)); System.out
.println("======================================="
); System.out
.println("方法二 (Math.round(x)*100)/100:"
+(Math.round(x*100
))/100.0f
); System.out
.println("方法二 (Math.round(y)*100)/100:"
+(Math.round(y*100
))/100.0f
); System.out
.println("方法二 (Math.round(z)*100)/100:"
+(Math.round(z*100
))/100.0f
); System.out
.println("======================================="
); BigDecimal bd =new
BigDecimal(x); BigDecimal bs = bd.setScale(2
, BigDecimal.ROUND_HALF_UP); System.out
.println("方法三 bs:"
+bs); BigDecimal bd2 =new
BigDecimal(y); BigDecimal bs2 = bd2.setScale(2
, BigDecimal.ROUND_HALF_UP); System.out
.println("方法三 bs2:"
+bs2); BigDecimal bd3 =new
BigDecimal(z); BigDecimal bs3 = bd3.setScale(2
, BigDecimal.ROUND_HALF_UP); System.out
.println("方法三 bs3:"
+bs3); System.out
.println("======================================="
); NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMaximumFractionDigits(2
); nf.setRoundingMode(RoundingMode.HALF_UP); System.out
.println("方法三 nf.format(x):"
+nf.format(x)); System.out
.println("方法三 nf.format(y):"
+nf.format(y)); System.out
.println("方法三 nf.format(z):"
+nf.format(z)); } }/<code>