【问题标题】:How to get a certain number of decimal points after the number? java如何在数字后获得一定数量的小数点?爪哇
【发布时间】:2014-07-08 17:57:33
【问题描述】:

我有一种打印时间的方法,例如: 1.23455 1.26789 1.27853

我希望它缩短到只有两位小数,例如: 1.23 1.26 1.27

      System.out.println("time: "+ time);

【问题讨论】:

标签: java floating-point numbers decimal


【解决方案1】:

很多很多很多方法...例如你可以使用String#format(String format, Object... arg)

System.out.println(String.format("time: %.2f", time));

或使用printf,或DecimalFormat。我让你用谷歌搜索它;)

【讨论】:

  • 如果这是您使用的解决方案,您应该接受他的回答。
  • 我不得不等待 12 分钟冷静
猜你喜欢
  • 2012-05-22
  • 1970-01-01
  • 2011-04-22
  • 1970-01-01
  • 1970-01-01
  • 2019-05-05
  • 1970-01-01
  • 2013-11-29
相关资源
最近更新 更多