Java Double to String conversion without formatting
Solution 1
Use Long:
long id = 654987;
String str = Long.toString(id);
Solution 2
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?
Solution 3
If it's an integer id in the database, use an Integer instead. Then it will format as an integer.
Solution 4
How about String.valueOf((long)value);
Solution 5
What about:
Long.toString(value)
or
new String(value)
Mizipzor
A crazy coder in a passionate hunt for greater wisdom. I take great interest in anything involving math and algorithms. Especially path finding, artificial life, cellular automata and emergent behavior.
Updated on November 05, 2020Comments
-
Mizipzor over 3 years
I have the number 654987. Its an ID in a database. I want to convert it to a string. The regular Double.ToString(value) makes it into scientific form, 6.54987E5. Something I dont want.
Other formatting functions Ive found checks the current locale and adds appropriate thousand separators and such. Since its an ID, I cant accept any formatting at all.
How to do it?
[Edit] To clarify: Im working on a special database that treats all numeric columns as doubles. Double is the only (numeric) type I can retrieve from the database.