<分区>
我想使用 Java 在 0 到 2.10 的范围内随机生成价格。我想要价格格式为 0.00、0.10、0.20、0.30、0.40、0.50 .... 1.10、1.20 一直到 2.10。我的代码是这样的:
private static DecimalFormat df2 = new DecimalFormat(".##");
double min = 0;
double max = 2.10;
double diff = max - min;
double randomValue = min + Math.random( ) * diff;
System.out.println(df2.format(randomValue));
但是,它打印出诸如 .06、1.0、.99、.02 等值。是否有格式化它的方法?