Percentage in android studio

0

hello friends that I have a doubt to get the percentage in Android is that I saw how it was done in android and it is supposed to be similar this is my code in android: Where I'm supposed to do this

sum2 = to a total of a previous operation ini = is the value I entered

and it is assumed that in java the percentage is removed like this

percentage = (number / 100) * amount; an example

someone can help me I get very high numbers

package com.example.danhermes.despensa;

import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast;

public class pantry extends AppCompatActivity implements View.OnClickListener {

EditText etdisponible, etcompra, etcantidad, etarticulo, etinicio;
Button btncomprar, btnaceptar;
TextView tvporcentaje;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.despensa);
    etdisponible = (EditText) (super.findViewById(R.id.etdisponible));
    etcompra = (EditText) (super.findViewById(R.id.etcompra));
    etcantidad = (EditText) (super.findViewById(R.id.etcantidad));
    etarticulo = (EditText) (super.findViewById(R.id.etarticulo));
    etinicio = (EditText) (super.findViewById(R.id.etinicio));

    btncomprar = (Button) (super.findViewById(R.id.btncomprar));
    btnaceptar = (Button) (super.findViewById(R.id.btnaceptar));

    tvporcentaje = (TextView) (super.findViewById(R.id.tvporcentaje));

    btncomprar.setOnClickListener(this);
    btnaceptar.setOnClickListener(this);

}

@Override
public void onClick(View view) {
    if (view.getId() == R.id.btnaceptar) {
        try {
            //tabla=Float.parseFloat(et_tabla.getText().toString());
            String inicio=etinicio.getText().toString();
            String articulo=etarticulo.getText().toString();
            String cantidad=etcantidad.getText().toString();

            int ini=Integer.parseInt(inicio);
            int art=Integer.parseInt(articulo);
            int cant=Integer.parseInt(cantidad);

            double suma=(ini-art*cant);
            String resultadosuma=String.valueOf(suma);
            etdisponible.setText(resultadosuma+ " Cantidad Restante");

            double suma2=(ini-suma);
            String resultadosuma2=String.valueOf(suma2);
            etcompra.setText(resultadosuma2+ " Total de Compras");


            double porcentaje=(suma2/100)*ini;
            String porciento=String.valueOf(porcentaje);
            tvporcentaje.setText(porciento);


        } catch (NumberFormatException e) {
            Toast toast = Toast.makeText(this, "Favor de escribir numeros", Toast.LENGTH_SHORT);
            toast.show();
        }

    }
    else if(view.getId()==R.id.btncomprar){
        Toast toast = Toast.makeText(this, "Favor de escribir Compras", Toast.LENGTH_SHORT);
        toast.show();
    }
}

}

    
asked by Dan Hermes Reyes Osnaya 24.02.2018 в 23:44
source

1 answer

-1

To obtain the% of a number you must use double for the amounts, and float to get the percentage itself.

Example: the% of 20, of a maximum amount of 100, would be 20%, if the maximum amount is 80, then the% would be 25.

EditText num,max;
TextView total;

double d1 = Double.parseDouble(num.getText().toString());
double d2 = Double.parseDouble(max.getText().toString());

float porciento = (float) ((d1/d2) *100);

String porcentaje = Float.toString(porciento);

total.setText(porcentaje);

In your case it would be like this:

double ini = Double.parseDouble(etinicio.getText().toString());
double art = Double.parseDouble(etarticulo.getText().toString());
double cant = Double.parseDouble(etcantidad.getText().toString());
double resta = ini-art*cant;

float porciento = (float) ((resta/ini) *100);

String porcentaje = Float.toString(porciento);

tvporcentaje.setText(porcentaje);

500 is 100%, 175 is 35%, 325 is 65%

    
answered by 25.02.2018 в 01:01