0

For a simple calculation like:

1+1 = 2.0

I'm not sure how to get rid of the unnecessary .0 after the 2 integer, even though I'm working with a double variable as my output. So, 1.1 + 1 = 2.1 is acceptable.

My output code looks like this:

        double total = 1+1;        
        output.setText(String.valueOf(total));
vacuum
  • 2,273
  • 3
  • 20
  • 32
JonAthan LAm
  • 89
  • 1
  • 1
  • 7

2 Answers2

3

Use java.text.NumberFormat or String.format(), e.g.:

output.setText(String.format("%.0f", total));
Moritz Petersen
  • 12,902
  • 3
  • 38
  • 45
0

You could also check with modulo for the value after the . , but I think you should work with the other solution mentioned by @MoritzPetersen .

           double total = 1+1.1;        
            if((total%1) == 0) {
                System.out.println((String.valueOf(total)).split("\\.")[0]);
            }
            else {
                System.out.println((String.valueOf(total)));
            }
Chris M
  • 52
  • 6