31

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.

4
  • 3
    See the top answer in, stackoverflow.com/questions/47045/sprintf-equivalent-in-java Answers the same question. Commented Oct 27, 2009 at 14:13
  • 1
    654987 is an integer, not a double. Did you mean something else? Commented Oct 27, 2009 at 14:14
  • check this: stackoverflow.com/questions/1521122/… Commented Oct 27, 2009 at 14:47
  • @JuanZe: Setting a locale causes thousand separators to be added, which I dont want. Commented Oct 27, 2009 at 15:18

9 Answers 9

33

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?

Sign up to request clarification or add additional context in comments.

2 Comments

Reason for double is that Im working with a database that treats all numeric columns as doubles.
If you cast double to long, sometimes the value changes. For ex: ((long) ((double)25891000000002001L)) becomes 25891000000002000L
20

Use Long:

long id = 654987;
String str = Long.toString(id);

Comments

6

If it's an integer id in the database, use an Integer instead. Then it will format as an integer.

1 Comment

In a perfect world, of course, but the reason for the question is that Im trying to work around limits in the database; the only (numeric) type it can store is a double.
6

How about String.valueOf((long)value);

Comments

2

What about:

Long.toString(value)

or

new String(value)

4 Comments

Please don't suggest ""+value! It's a hack and doesn't convey the intent in any way. I'd say it's bad style at the very least.
Ok :) Replaced that with "new String(value)".
new String(value) will have the same problem as the question mentions, if value is a double.
new String(long) does not exist in Java
2

Also you can use

double value = getValue();

NumberFormat f = NumberFormat.getInstance();
f.setGroupingUsed(false);

String strVal = f.format(value);

Comments

1

If what you are storing is an ID (i.e. something used only to identify another entity, whose actual numeric value has no significance) then you shouldn't be using Double to store it. Precision will almost certainly screw you.

If your database doesn't allow integer values then you should stored IDs as strings. If necessary make the string the string representation of the integer you want to use. With appropriate use of leading zeros you can make the alphabetic order of the string the same as the numeric order of the ints.

That should get you round the issue.

1 Comment

Sadly, both the database and its datatypes are set. The column I am to handle is a double and there is nothing I can do about it. When the database is read I of course quickly convert it into an int for internal use (or a string as you suggest). It doesnt really matter since the problem is the actual conversion, as I cant change the content or layout of the database.
0

What about Long.toString((long)value) ?

Comments

-3
    double d = 56789;
    String s = d+"";

1 Comment

Doesnt that call Double.toString(d)? Which by default, if Im not mistaken, adds both scientific notation and thousand separators (if necessary).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.