2 decimal place using double [duplicate]

3

I'm learning to program, I'm doing an application with 3 edittext (where I enter the numbers I want to calculate) and a textview to display the result. I would like to know how to calculate 2 numbers with option to be able to fill the third edittext. And I would like to know how I can convert my result to 2 decimal places.

I leave here the code:

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 + "€" );




    }
    }
    
asked by anonymous 21.09.2017 / 12:14

2 answers

0

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

Well it may not be exactly what you're looking for but I think from these modifications you can do what you're looking for

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) + "€" );



    }
}
    
21.09.2017 / 12:51
3

You can use NumberFormat along with DecimalFormat .

As for validation, you just want to validate two values, but in the condition you had the three values to validate. 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) + "€");
    
21.09.2017 / 12:39