Convert Double to Integer

-1

I'd like the value of "trees to reset" to appear with no point, nor zeros. In the situation below appears "180.0000000" and should appear "180". It is of type TextView , and would like it to appear in the Integer type. I have already tried to convert using the method Double , of class Format , but nothing solved so far

    
asked by anonymous 12.10.2018 / 04:27

3 answers

2

See if this example helps you.

double pi = 3.14159;
int i = (int)pi;
    
12.10.2018 / 04:51
0
  

I have already tried to convert using the method Format , of class String

That also works, as does the @RenanOsorio answer.

Using String.format " only needs to set the formatter as %.0f to display without any decimal places:

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

textArvoresRepor.setText(String.format("%.0f", arvoresRepor));
    
12.10.2018 / 12:25
0

You can use NumberFormat and onCreate() you initialize and configure it, and then use that 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: The NumberFormat is of the java.text package; So you'll have to import it.

    
13.10.2018 / 09:25