【发布时间】:2020-05-29 20:37:07
【问题描述】:
private static DecimalFormat df = new DecimalFormat("0.00");
public static void main(String[] args) {
double input = 300.00;
System.out.println("double : " + input);
Double d=Double.valueOf(df.format(input));
System.out.println("##1::"+d);
System.out.println("##2 ::"+df.format(input));
System.out.println("##3 ::"+Double.valueOf(df.format(input)));
}
}
输出:
double : 300.0
##1::300.0
##2 ::300.00
##3 ::300.0
预期输出:300.00
【问题讨论】:
-
//double : 300.0 //##1::300.0 //##2 ::300.00 //##3 ::300.0 //double vale 的预期输出为 300.00(不想要转换为字符串)
-
什么意思?在第 2 种情况下,使用
DecimalFormat,您的值将打印为300.00,就像您想要的那样。 -
只是想将此 DecimalFormat 值分配给其他变量 Double b 这不可能吧? Double.valueOf(df.format(input)
-
这有什么奇怪的?为什么您希望
double跟踪十进制零的数量? -
@MayankL 一般人都明白 100.00 和 100.0 是一回事。如果您真的关心格式,则可以将
DecimalFormat对象传递到显示号码的任何位置。更好的是,使用BigDecimal