Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
356 views
in Technique[技术] by (71.8m points)

android - How to Avoid Scientific Notation in Double?

Here is my simple code

        @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("");
        }
    }

});

This is a simple code for Percentage Calculator.

What I want is to avoid the Scientific Notation in my Calculator cause I don't want to explain to user what is Scientific Notation.

For example if I want to calculate 100,000,000 and cut 50% of it, it Should give me 50,000,000 which is giving me 5.0E7 And in my case this doesn't make any sense to the user. And of course I know both results are correct. Thanks in Advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Check answer here. You can write

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...