<分区>
我是 Java 编程的新手。我想以 5 的倍数将价格四舍五入到最接近的小数点后两位。
例如
38.80
保持不变。38.81
到38.80
。38.82
到38.80
。38.83
到38.85
。38.84
到38.85
。38.85
保持不变。38.86
到38.85
。38.87
到38.85
。38.88
到38.90
。38.89
到38.90
。38.90
保持不变。
我尝试了提供的副本,但它们只显示到小数点后一位。
例如。 38.82
到 38.8
。
这是我的代码:
import java.text.DecimalFormat;
public class RoundUp {
public static void main(String[] args){
DecimalFormat df = new DecimalFormat("#.##");
double num = 38.84;
System.out.println(df.format(Math.round(num*10.00)/10.00));
}
}
我已经研究了该网络中专家的其他模型答案,但没有一个能真正回答我的问题。设置为小数点后两位,我使用的是 DemicalFormat
。我知道,但四舍五入,假设 38.83 到 38.85 和 38.87 到 38.90 是我真正想要的。
这是我国正在使用的舍入系统。可以看看here .
**
This question has been answer by @Mureinik double rounded = Math.round(num * 100.0 / 5.0) * 5.0 / 100.0;
**