7

I am trying to display numbers in a string dynamically, so if the number has decimal's display them but if not don"t show the .0

example: display 5.5 as 5.5 and 5.0 as 5

This is what I have so far: (answer is a double)

double temp = answer;
long temp2 = (long) temp;
if (temp == temp2) {
output = String.valueOf(temp2);
System.out.println(output);

this work's fine up to about 1e18 then will error out because of the maximum size of a Long. So how would I achieve this on bigger numbers like 5.43e86

Josh
  • 315
  • 1
  • 3
  • 8

5 Answers5

27

Use DecimalFormat

double answer = 5.0;
DecimalFormat df = new DecimalFormat("###.#");
System.out.println(df.format(answer));
Kal
  • 24,724
  • 7
  • 65
  • 65
  • 1
    I think you mean `new DecimalFormat("###");` because he does not want to see the digits after decimal dot. – AlexR Aug 18 '11 at 14:54
  • 1
    Thanks! I am using devimalformat elsewhere in my application. Should have thought of that. great answer. – Josh Aug 18 '11 at 14:55
  • @AlexR -- That will print 5 ( instead of 5.0 ) and 5.5 if answer = 5.5. – Kal Aug 18 '11 at 14:55
  • -1, # is "Digit, zero shows as absent", which will still print the 0 after the decimal. Not what he asks – GBa Aug 18 '11 at 14:56
  • @Greg -- I suggest you try the code above. It does exactly what the OP is asking. – Kal Aug 18 '11 at 14:57
2

It's not good solution if you use new DecimalFormat("0.#") you are missing data, for example PI = 3.14, but after parse you ae geting 3.1

Another solution to use eval%1 ? (int)d : d this time couse max integer limit , again missing data

my solution is working, but it's not good idea

res = removeLastChars(eval,".0");

private String removeLastChars(double eval, String text){

    String res = String.valueOf(eval);
    int length = text.length();

    if (res.length() > length){
        res = res.substring((res.length() - length), res.length()).equals(text)
                ? res.substring(0, (res.length() - length)) : res;
    }

    return res;
}
Vahe Gharibyan
  • 5,277
  • 4
  • 36
  • 47
2

The DecimalFormat suggestions are the easiest way to handle this. If they aren't sufficient, here's another idea.

If you're starting to hit the maximum values that can be represented by primitives in Java, then you may need to move to BigInteger and BigDecimal.

Try playing around with the BigDecimal.toBigInteger() method coupled with the toString() methods on BigDecimal and BigInteger.

Ophidian
  • 9,775
  • 2
  • 29
  • 27
0

Using Apache commons-lang:

StringUtils.removeEnd(String.valueOf(100d), ".0")

gives you "100" instead of "100.0":

import org.apache.commons.lang3.StringUtils;

System.out.println(String.valueOf(100d)); // output is "100.0"
System.out.println(StringUtils.removeEnd(String.valueOf(100d), ".0")); // output is "100"
Igor Bljahhin
  • 939
  • 13
  • 23
-3

Look at http://download.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html

you would want just DecimalFormat("0.0")

GBa
  • 17,509
  • 15
  • 49
  • 67