How to limit the number of decimals in Java

Asked

Viewed 4,461 times

-1

Hello, in this section I take a double multiplic value to 0.3 and display in a textview, as I show the result with 2 decimal places?

deslocamento.setOnClickListener( new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            try{
                if (deslocamento.isChecked()){
                    double valorDeslocamento1 = Double.parseDouble(valorViagem.getText().toString())*0.3;
                    valorDeslocamento.setText( String.valueOf(valorDeslocamento1));
            }
        }
    } );

2 answers

1

Numberformat Formatter = new Decimalformat("#0.00");
System.out.println(Formatter.format(2.0));

  • Paul Thank you God bless you

1

TextView tv = //sua textView
String value = tv.getText().toString();
double n = Double.parseDouble(value) * 0.3;
String result = String.format("%.2f", n);
//.2 é a precisão do número e %f é a máscara para números de ponto flutuante
  • Peter Thank you so much God bless you.

Browser other questions tagged

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