Greed Ruler
Greed Ruler

Reputation: 167

android: calculate double percentage?

I want to calculate percentage. But it's giving me wrong result(2.78674E7).

Following's my code:

public class DetailActivity extends AppCompatActivity {

    private Button btn_pre_payment;
    private EditText et_pre_payment;

    private double monthlyPayment;
    private double allPayment = 139337000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        btn_pre_payment = (Button)findViewById(R.id.bt_prepayment);
        et_pre_payment = (EditText)findViewById(R.id.et_prepayment);

        btn_pre_payment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(et_pre_payment.getText().length() > 0){

                    float value = Float.valueOf(et_pre_payment.getText().toString());
                    if(value >= 10){

                        InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        in.hideSoftInputFromWindow(et_pre_payment.getApplicationWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);


                            **monthlyPayment = (allPayment / 100f) * value;
                            tvMonthlyPaymentH.setText(String.valueOf(monthlyPayment));
                            remainderPayment -= monthlyPayment;**

                        }

                    } else Toast.makeText(DetailActivity.this, "Low is 10%!", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(DetailActivity.this, "Enter value!", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

Why can't it calculate correct result? It gives me 2.78674E7. I want to calculate percentage.

Upvotes: 0

Views: 2346

Answers (2)

Manohar
Manohar

Reputation: 23404

The value is given in scientific notation you can try using

String.format("%.0f", YourValue)

to get correct value .

check out this question and this question

Upvotes: 0

Gabe Sechan
Gabe Sechan

Reputation: 93668

For starters, your math is wrong. It should be value/total*100, not value*total/100.

Upvotes: 1

Related Questions