如何避免双重科学记数法?

时间:2013-12-28 10:47:09

标签: android double

这是我的简单代码

        @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 在我的情况下,这对用户没有任何意义。当然,我知道两种结果都是正确的。 在此先感谢。

5 个答案:

答案 0 :(得分:12)

检查回答here。你可以写

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

答案 1 :(得分:6)

您可以尝试使用此DecimalFormat

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

答案 2 :(得分:2)

我建议使用BigDecimal代替double s。这样您就可以更精确地控制计算精度。您还可以使用String获取非科学BigDecimal.toPlainString()

答案 3 :(得分:1)

DecimalFormat decimalFormatter = new DecimalFormat("##.############");
                    decimalFormatter.setMinimumFractionDigits(2);
                    decimalFormatter.setMaximumFractionDigits(15);

此选项将帮助您##。##后缀0在十进制之前,否则输出将为.000

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

用它来显示内容

答案 4 :(得分:0)

使用NumberFormater,如

NumberFormat myformatter = new DecimalFormat("########");  

String result = myformatter.format(yourValue);
相关问题