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();
Answer from Paco Abato on Stack OverflowIf 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();
You could try this:
String numWihoutDecimal = String.valueOf(documentNumber).split("\\.")[0];
How to remove decimal values from a value of type 'double' in Java - Stack Overflow
Java Double to String conversion without formatting - Stack Overflow
converting Double to string
How to convert string to double without losing precision after decimal point?
How can I control the number of decimal places when converting a double to a string in Java?
What's the difference between using String.valueOf() and Double.toString() when converting a double to a string in Java?
How does Java handle the conversion of a negative double to an int?
Use a fixed NumberFormat (specifically a DecimalFormat):
double value = getValue();
String str = new DecimalFormat("#").format(value);
alternatively simply cast to int (or long if the range of values it too big):
String str = String.valueOf((long) value);
But then again: why do you have an integer value (i.e. a "whole" number) in a double variable in the first place?
Use Long:
long id = 654987;
String str = Long.toString(id);
double total = 44;
String total2 = String.valueOf(total);
This will convert double to String
Using Double.toString(), if the number is too small or too large, you will get a scientific notation like this: 3.4875546345347673E-6. There are several ways to have more control of output string format.
double num = 0.000074635638;
// use Double.toString()
System.out.println(Double.toString(num));
// result: 7.4635638E-5
// use String.format
System.out.println(String.format ("%f", num));
// result: 0.000075
System.out.println(String.format ("%.9f", num));
// result: 0.000074636
// use DecimalFormat
DecimalFormat decimalFormat = new DecimalFormat("#,##0.000000");
String numberAsString = decimalFormat.format(num);
System.out.println(numberAsString);
// result: 0.000075
Use String.format() will be the best convenient way.
If the idea is to print integers stored as doubles as if they are integers, and otherwise print the doubles with the minimum necessary precision:
public static String fmt(double d)
{
if(d == (long) d)
return String.format("%d",(long)d);
else
return String.format("%s",d);
}
Produces:
232
0.18
1237875192
4.58
0
1.2345
And does not rely on string manipulation.
String.format("%.2f", value);
Your problem belongs to Locale, as pointed out correctly by Rorick. However, you should look into DecimalFormat class, in case changing Locale means mess up all the things.
Look at NumberFormat class, to deal with thousand separator. Because it seems your case is regarding thousand separator instead.
This will remove all grouping (in your case commas).
DecimalFormat df = new DecimalFormat();
df.setGroupingUsed(false);