如何将3个数字舍入为1?

时间:2014-04-20 18:20:57

标签: java android timer

private Runnable updateTimerThread = new Runnable() {

        public void run() {

            timeInMilliseconds = SystemClock.uptimeMillis() - startTime;

            updatedTime = timeSwapBuff + timeInMilliseconds;

            int secs = (int) (updatedTime / 1000);
            int mins = secs / 60;
            secs = secs % 60;
            int milliseconds = (int) (updatedTime % 1000);
            timerValue.setText("" + mins + ":"
                    + String.format("%02d", secs) + ":"
                    + String.format("%03d", milliseconds));
            customHandler.postDelayed(this, 0);
        }

    };

当前结果= 00.15.546,期望结果00.15.5
毫秒是我希望1位数的3位数

1 个答案:

答案 0 :(得分:2)

If(milliseconds %10<50)
 milliseconds = milliseconds/100;
else
 milliseconds = milliseconds/100 + 1;

从头开始它会给你一个十进制数,因为计时器不是我的坏人。尝试类似的东西:     Math.round(毫秒* 100)/ 100; \