【问题标题】:Require output always of format 2 digit to right and 7 digit to left of decimal始终要求输出格式为小数点右侧 2 位和左侧 7 位
【发布时间】:2013-01-09 16:42:17
【问题描述】:

我想要输出经纬度双值的字符串,小数点前为 1,2 或 3 位,小数点后为 7 位。如何在 java 中使用 DecimalFormat() 来做到这一点?

    12.456789   should give 12.4567890
    12.45678912 should give 12.4567891

提前致谢

【问题讨论】:

  • 所以 1.2345 应该是 01.2345000 ? -160.1234567 可以吗?
  • @PeterLawrey 是的你,我只想要小数点后 7 位数字
  • 但是您需要在小数点前精确两位数,还是可以是 1,2 或 3?
  • @PeterLawrey 应该是 1,2 或 3

标签: java android decimal


【解决方案1】:

试试

String s = new DecimalFormat("00.0000000").format(12.456789);

此版本具有固定的整数长度,或

  String s = new DecimalFormat("0.0000000").format(12.456789);

具有浮动长度

【讨论】:

  • 这是否总是返回小数点后 7 位数字?
【解决方案2】:

String formatted = new DecimalFormat("##.######").format(input);

【讨论】:

    【解决方案3】:

    我只想要小数点后的 7 位数字

    这将始终为您提供 7 个小数位,根据需要四舍五入。

    String s = String.format("%.7f", lat);
    

    同时做这两件事

    String s = String.format("lat %.7f, long %.7f", lat, long);
    

    【讨论】:

      猜你喜欢
      • 2021-03-28
      • 1970-01-01
      • 1970-01-01
      • 2014-10-26
      • 2015-08-29
      • 1970-01-01
      • 2016-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多