【问题标题】:How to Avoid Scientific Notation in Double?如何避免双重科学记数法?
【发布时间】:2013-12-28 10:47:09
【问题描述】:

这是我的简单代码

        @Override
    public void onClick(View v) {
        try {
            double price = Double.parseDouble(ePrice.getText().toString());
            double percent = Double.parseDouble(ePercent.getText().toString());

            double priceValue = price * percent/100.0f;
            double percentValue = price - priceValue;

            moneyToGet.setText(String.valueOf(priceValue));
            moneyToPay.setText(String.valueOf(percentValue));

            moneyToGet.setText("" + priceValue);
            moneyToPay.setText("" + percentValue);

            // catch
        } catch (NumberFormatException ex) {
            // write a message to users
            moneyToGet.setText("");
        }
    }

});

这是百分比计算器的简单代码。

我想要避免在我的计算器中使用科学记数法,因为我不想向用户解释什么是科学记数法。

例如如果我想计算100,000,000并削减50%,它应该给我50,000,000 这给了我 5.0E7 在我的情况下,这对用户没有任何意义。当然,我知道这两个结果都是正确的。 提前致谢。

【问题讨论】:

标签: android double


【解决方案1】:

检查答案here。你可以写

moneyToGet.setText(String.format("%.0f", priceValue));

【讨论】:

  • 请注意,如果您的号码有小数部分,它会删除它。
【解决方案2】:

你可以试试这个 DecimalFormat

DecimalFormat decimalFormatter = new DecimalFormat("############");
number.setText(decimalFormatter.format(Double.parseDouble(result)));

【讨论】:

  • 它现在正在工作......非常感谢你们的帮助。
【解决方案3】:

我建议使用BigDecimals 而不是doubles。这样,您将对计算精度进行更精确的控制。您也可以使用BigDecimal.toPlainString() 获得非科学的String。

【讨论】:

    【解决方案4】:
    DecimalFormat decimalFormatter = new DecimalFormat("##.############");
                        decimalFormatter.setMinimumFractionDigits(2);
                        decimalFormatter.setMaximumFractionDigits(15);
    

    此选项将帮助您##.## 小数点前后缀 0,否则输出将是 .000

     btc.setText(decimalFormatter.format(btcval));
    

    用它来显示内容

    【讨论】:

      【解决方案5】:

      像使用 NumberFormater 一样

      NumberFormat myformatter = new DecimalFormat("########");  
      
      String result = myformatter.format(yourValue);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-13
        • 2015-09-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多