Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Avoid Scientific Notation in Double?

Tags:

android

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.

like image 438
user3051834 Avatar asked Dec 28 '13 10:12

user3051834


People also ask

How do you avoid scientific notation?

(1) Right-click a cell where you want to remove scientific notation, and (2) choose Format Cells… 2. In the Format Cells window, (1) select the Number category, (2) set the number of decimal places to 0, and (3) click OK.

How do you get rid of double E?

Just to add to what Joachim said, you can't "remove E from a double" because double doesn't have E. Double is just a binary number. It is up to the formater to display that number with E or not.

How do I print a double value without scientific notation using Java?

double dexp = 12345678; System. out. println("dexp: "+dexp);

How do you change back to scientific notation?

To convert any number into scientific notation, you write the non-zero digits, placing a decimal after the first non-zero digit. Then, you count the number of digits you need to move the beginning decimal to get to where your decimal is now. If you move the decimal to the left, then your power is positive.


1 Answers

Check answer here. You can write

moneyToGet.setText(String.format("%.0f", priceValue));
like image 146
Josef Raška Avatar answered Sep 30 '22 02:09

Josef Raška