【问题标题】:Fomat float and double into scientific notation in specific format given below以下面给出的特定格式将 float 和 double 格式化为科学计数法
【发布时间】:2019-11-13 04:53:20
【问题描述】:

我已经在这里问了一个问题,虽然它没有解决我的问题,但它被标记为重复,我试图重新打开,但它没有成功。 Forcing BigDecimals to use scientific notation

format decimal number to specific exponential format in java

所以在将我的问题标记为重复之前,请先查看它是否与我的问题有关。

问题:

给定一个浮点数或双精度数操作成特定格式 例如:13.33 = 0.1333 E+02 0.00023 = 0.23 E-03

数字的非零位从小数点后开始。

如何使用十进制格式来实现这个输出。

【问题讨论】:

  • 顺便说一句,如果你想通知某人,你可以使用@username 来做到这一点
  • 我不想四舍五入。是的,我可以使用 BigDecimal,但我无法弄清楚如何在开头强制小数点
  • @ScaryWombat 我昨天试过并写了评论,但是当我输入 @ 时,您的用户名没有出现

标签: java floating-point double decimal exponential


【解决方案1】:

如你所愿,数字的非零位从小数点后开始。

你可以的

    BigDecimal x = new BigDecimal("13.33");
    DecimalFormat frmt = new DecimalFormat(".00E00");
    String formatted = "0" + frmt.format(x.doubleValue());
    System.out.println("result: " + formatted);

结果

13.33
result: 0.13E02
0.00023
result: 0.23E-03

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    • 2011-10-31
    相关资源
    最近更新 更多