2 decimal places using double

Asked

Viewed 2,465 times

3

I am learning to program, I am making an application with 3 edittext (where I insert the numbers I want to calculate) and a textview to present the result. I would like to know how to calculate 2 numbers with option to fill the third edittext. And I wonder how I can convert my result to 2 decimal places.

I leave the code here:

public void somar (View v){

        EditText odd1 = (EditText) findViewById(R.id.editText7);
        String stringodd1 = odd1.getText().toString();

        EditText odd2 = (EditText) findViewById(R.id.editText8);
        String stringodd2 = odd2.getText().toString();

        EditText odd3 = (EditText) findViewById(R.id.editText9);
        String stringodd3 = odd3.getText().toString();

        EditText montante = (EditText) findViewById(R.id.editText12);
        String stringmontante = montante.getText().toString();


        if (stringodd1.trim().isEmpty() || stringodd2.trim().isEmpty() || stringodd3.trim().isEmpty())
        {
            Toast.makeText(getApplicationContext(), "Campos em branco",
                    Toast.LENGTH_LONG).show();
        }
        else
        {

            double valorodd1 = Double.parseDouble(stringodd1);
            double valorodd2 = Double.parseDouble(stringodd2);
            double valorodd3 = Double.parseDouble(stringodd2);
            double valormontante = Double.parseDouble(stringmontante);


            double resu = valorodd1 * valorodd2 * valorodd3 * valormontante;



            TextView resultado = (TextView) findViewById(R.id.textView15);
        resultado.setText (resu + "€" );




    }
    }

2 answers

3

Can use a NumberFormat along with DecimalFormat.

As for the validation, you only want to validate two values, but in the condition you had the three values to be validated. So I removed the validation of the third number:

import java.text.DecimalFormat;
import java.text.NumberFormat;
...
NumberFormat formatter = new DecimalFormat("#0.00");

if (stringodd1.trim().isEmpty() || stringodd2.trim().isEmpty())
...
resultado.setText(formatter.format(resu) + "€");
  • It is done :). Regarding the edittext calculation, we imagine for example 5 boxes where we insert 5 numbers and multiply these 5 numbers and get the result, my idea is to have the option to multiply for example only 2 boxes

0


Hi, I made some modifications to your code. The user can enter a number or more that will be made the calculation, I modified its condition not to do operation when the 3 entries are null( but if the if the code also works ). So the user can type in any of the edittext that will work( I did it because I didn’t understand how I wanted to). I inserted the mask to format the output to have only 2 decimal places.

Good may not be exactly what you want but I think from these modifications you can do what you want

what is a ternary operator

public void somar (View v){

    NumberFormat formatter = new DecimalFormat("#0.00");

    EditText odd1 = (EditText) findViewById(R.id.editText7);
    String stringodd1 = odd1.getText().toString();

    EditText odd2 = (EditText) findViewById(R.id.editText8);
    String stringodd2 = odd2.getText().toString();

    EditText odd3 = (EditText) findViewById(R.id.editText9);
    String stringodd3 = odd3.getText().toString();

    EditText montante = (EditText) findViewById(R.id.editText12);
    String stringmontante = montante.getText().toString();

    double valorodd1,valorodd2,valorodd3;

    //verificando se valores sao nulos
    stringodd1.trim().isEmpty() ? valorodd1 = 1 : valorodd1 = Double.parseDouble(stringodd1);
    stringodd2.trim().isEmpty() ? valorodd2 = 1 : valorodd2 = Double.parseDouble(stringodd2);
    stringodd3.trim().isEmpty() ? valorodd3 = 1 : valorodd3 = Double.parseDouble(stringodd2);
    // verificando se todos os valores são nulos obs: esse controle pode ser retirado que a logica também funcionará 
    if (stringodd1.trim().isEmpty() && stringodd2.trim().isEmpty() && stringodd3.trim().isEmpty())
    {
        Toast.makeText(getApplicationContext(), "Campos em branco",
                Toast.LENGTH_LONG).show();
    }
    else
    {
        double valormontante = Double.parseDouble(stringmontante);


        double resu = valorodd1 * valorodd2 * valorodd3 * valormontante;


        TextView resultado = (TextView) findViewById(R.id.textView15);
        resultado.setText (formatter.format(resu) + "€" );



    }
}
  • Good afternoon, I tried to follow your edition, I was making a mistake in the "'?" in: stringodd1.Trim(). isEmpty() ? valued1 = 1 : valued1 = Double.parseDouble(stringodd1); stringodd2.Trim(). isEmpty() ? valued2 = 1 : valued2 = Double.parseDouble(stringodd2); stringodd3.Trim(). isEmpty() ? valuodd3 = 1 : valued3 = Double.parseDouble(stringodd2); what can I do? Thank you

  • if(stringodd1.Trim().isEmpty()){valued1 = 1; } Else{valueDouble = Double.parseDouble(stringodd1);} for each ternary operator

  • I got a friend. Thank you all for your help :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.