【问题标题】:How to set precision of double values to be of the form ##.## at ALL times?如何始终将双精度值的精度设置为 ##.## 形式?
【发布时间】:2012-12-18 00:14:24
【问题描述】:

我目前正在尝试为始终具有 ##.## 形式的 Android 游戏创建计时器。

我遇到的问题是,如果我的计时器达到 23.50,它将设置为 23.5

所以,我试图将我的 double 值的精度设置为精确到小数点后 2 位。到目前为止,这是我的代码,它不起作用。任何建议都非常感谢,谢谢。

public void onTick(long millisUntilFinished) {
    double time1 = (double) millisUntilFinished/1000;
    DecimalFormat df = new DecimalFormat("##.##");
    timerDisplay.setText(df.format(time1));
}

【问题讨论】:

    标签: java formatting double precision


    【解决方案1】:

    你可以使用:

    DecimalFormat df = new DecimalFormat("#0.00");
    

    【讨论】:

      猜你喜欢
      • 2013-01-28
      • 1970-01-01
      • 2021-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多