Convert Double to Integer

Asked

Viewed 1,432 times

-1

I would like that in the TextView where the value of "trees to restore" appears without a point or zeros. In the situation below appears "180.0000000" and should appear "180". Is he the type Double, and would like it to appear in Integer type. I have tried converting using the class method Format, class String, but nothing solved so far

inserir a descrição da imagem aqui

  • 1

    And why would you do that? Any use of double for monetary value is wrong: https://answall.com/q/219211/101.

3 answers

2

See if that example helps you.

double pi = 3.14159;
int i = (int)pi;

0

I have tried converting using the method class Format, class String

Which also works, as does the @Renanosorio response.

Using String.format just need to set the formatter as %.0f to show without any decimal place:

double arvoresRepor = ...;
TextView textArvoresRepor = findViewById(...);

textArvoresRepor.setText(String.format("%.0f", arvoresRepor));

0

You can use the NumberFormat and in the onCreate() you initialize and configure it and then use this object to format the numbers. For example:

public class MainActivity extends AppCompatActivity {

    NumberFormat f;

    @Override
    public void onCreate() {

    f = NumberFormat.getNumberInstace();
    f.setMaximumFractionDigits(2); //No máximo 2 casas decimais

    //Inicialização da sua TextView e outros códigos...

    textArvoresRepor.setText(f.format(arvoresRepor)); //definindo o texto e formatando o mesmo.


    }

}

Detail: Numberformat is from the package java.text; So you’ll have to import it.

  • It worked, thank you very much!!

Browser other questions tagged

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