Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Double to String conversion without formatting

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.

like image 548
Mizipzor Avatar asked Oct 27 '09 14:10

Mizipzor


People also ask

How do I print a double value without scientific notation using Java?

double dexp = 12345678; System. out. println("dexp: "+dexp);

How do I remove decimal places from String in Java?

String rounded = String. format("%. 0f", doubleValue);

How do I convert a double array to a String?

You can convert a double array to a string using the toString() method. To convert a double array to a string array, convert each element of it to string and populate the String array with them.


2 Answers

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?

like image 96
Joachim Sauer Avatar answered Sep 24 '22 17:09

Joachim Sauer


Use Long:

long id = 654987; String str = Long.toString(id); 
like image 37
alphazero Avatar answered Sep 24 '22 17:09

alphazero