在 Android 中将小数四舍五入为 1.00 和 0.10

Rounding the Decimal to 1.00 and 0.10 in Android

如题,1.01,1.02,1.03,1.04如何四舍五入为1.00,0.05,0.06,0.07,0.08,0.09如何四舍五入为0.10

您可以试试下面的代码:

 private static void roundOff(double num) {
    double intPart = (int) num;
    double rondededOffNum = intPart+ Math.round((num- intPart)*10)/10.0;
    System.out.println(rondededOffNum);
}