10

What is the best way to convert a double to String without decimal places?

What about String.valueOf((int) documentNumber)?

The doubles always have 0 after the decimal dot. I don't need to round or truncate

John Alexander Betts
  • 4,718
  • 8
  • 47
  • 72

8 Answers8

15

If you are sure that the double is indeed an integer use this one:

NumberFormat nf = DecimalFormat.getInstance();
nf.setMaximumFractionDigits(0);
String str = nf.format(documentNumber);

As a bonus, this way you keep your locale's configuration as in thousand separator.

EDIT
I add this previously removed option as it seems that was useful to the OP:

Double.valueOf(documentNumber).intValue();
Paco Abato
  • 3,920
  • 4
  • 31
  • 54
  • It seemed to me that it was going to give the same result as a casting to int. I'm glad that you saw it and it worked for you. I re-add the removed answer as it seems to be useful. – Paco Abato Feb 25 '15 at 13:37
7

You could try this:

String numWihoutDecimal = String.valueOf(documentNumber).split("\\.")[0];
kuki
  • 71
  • 1
4

You can convert a double to string with the minimum necessary precision:

public static String ceonvert(double d)
{
    if(d == (long) d)
        return String.format("%d",(long)d);
    else
        return String.format("%s",d);
}

Or this :

> new DecimalFormat("#.##").format(2.199); //"2.2"
Hann
  • 727
  • 3
  • 11
  • 22
1

I'm not sure if this is best way, but i'am sure it's shortest way:

((int)documentNumber) + ""
Piotr Zych
  • 483
  • 4
  • 19
  • 1
    First we cast double to integer and than we convert integer by adding empty string to string. 3 simple operations. Maybe one bad point is that, we create one excess object (empty string), but java compiler is optimizing such construction. Even such object's like empty strings are cashed at start of JVM. In my opinion it is best practise: simple, easy-readable and easy to optimize for JVM. Prove me wrong. – Piotr Zych Feb 25 '15 at 20:31
  • there is nothing wrong with this solution if your soul purpose is to remove decimal from your double. – Wajid Jun 21 '18 at 10:23
0

Given the newly edited question stating that the doubles are in fact integers, I'd say that your suggested answer String.valueOf((int) documentNumber) is great.

Kirby
  • 15,127
  • 10
  • 89
  • 104
0

The easiest way to convert a double to string is to use quotation marks and then the double after that. double d = 123; String a = "" + d;

Another way is to use the toString method if you want to keep the way you converted it hidden

public String toString()
{
   String a + ""  d;
} 
0

I had a similar issue where Gson converted what should have been an Integer value to a Double and I wanted to write it as a String. NumberFormat works, but you also need to disable the grouping delimiter otherwise it outputs 1,234,567:

Object num = new Double(1234567);
NumberFormat nf = DecimalFormat.getIntegerInstance();
nf.setGroupingUsed(false);
String str = nf.format((Number)num);
-3

Quotation Marks + Double

String s = "" + 0.07;

LOL this is the best way, (obviously for experienced programmers)!