1

Consider

Double f = 2.0;
String s = f.toString();

The resulting string s is "2.0". How can I format f so the answer for the case where f doesn't have a decimal part doesn't have the .0 appended? (For cases where f has a decimal part, I need that to be included.). Yes, for some reason, the toString() method on a Double appends .0 for the case where the Double is an integer.

A nasty hack would be

f % 1.0 == 0.0 ? new Integer(f.intValue()).toString() : f.toString();

but to me that looks terrible.

krsteeve
  • 1,794
  • 4
  • 19
  • 29
Xavier Imbs
  • 212
  • 2
  • 10

5 Answers5

2

if I understand your question properly, you may try this:

    double f = 2.0;
    int i = (int)f;
    if (i==f){
        String s = String.valueOf(new DecimalFormat("#").format(f));
        System.out.println(s);
    }
    else{
        String s = String.valueOf(new DecimalFormat("#.###").format(f)); // for example
        System.out.println(s);
    }
}

//now if the number after the point is fully zero, it will not take it

results

f=2.0 -> s=2
f=2.005 -> s=2.005

EDIT: in a few lines if there is no specific "number of digits" after decimal -> you can use the ternary operator like this

double f = 2.000;
int i = (int)f;
String s = (f==i)? String.valueOf(i): String.valueOf(f);
System.out.println(s);
Yahya
  • 13,349
  • 6
  • 30
  • 42
0

You can use a several help methods of several classes to achieve the same result:

    Double f = 2.0;
    String s = null;
    // option1: https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#valueOf-int-
    s = String.valueOf(f.intValue());
    System.out.println(s);
    // option2: https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html#toString-int-
    s = Integer.toString(f.intValue());
    System.out.println(s);
    // option3: https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#format-java.lang.String-java.lang.Object...-
    s = String.format("%.0f", f);
    System.out.println(s);
    // option4: https://docs.oracle.com/javase/8/docs/api/java/text/DecimalFormat.html#DecimalFormat-java.lang.String-
    s = new DecimalFormat("#").format(f);
    System.out.println(s);

for an input like:

Double f = 21231.232222323;

the output will be: 21231

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

Using the number format (for more advanced formatting)

NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
System.out.println(nf.format(f));
gusto2
  • 11,210
  • 2
  • 17
  • 36
0

It seems there is no trivial formatter that does this. Using (thanks to @Thomas for the simplification of the integer case)

f % 1.0 == 0.0 ? String.valueOf(f.intValue()) : f.toString();

seems to be the best way.

f % 1.0 is a good way to test for integral f in Java.

Xavier Imbs
  • 212
  • 2
  • 10
0

As suggested in the comments, try DecimalFormat.

static void show(double v){
    DecimalFormat format = new DecimalFormat("##########.##");
    System.out.println(format.format(v));
}
public static void main (String[] args) throws java.lang.Exception
{
    show(1.1);
    show(1.0);
    show(1.15);
    show(1.116);
}

The out put will be:

1.1

1

1.15

1.12

Which seems like what you're looking for.

matt
  • 10,892
  • 3
  • 22
  • 34