2

How can i concatenate Double to String in java?

I have following piece of code:

Double price = 5.34;
String trade = MSFT;

now I have String key = price + trade, not sure if this is right way of doing it, any suggestions?

Rachel
  • 100,387
  • 116
  • 269
  • 365
  • 3
    What you propose does exactly that, it concatenates the double and the string. What is your question? Do you need a specific format for the double? – assylias Aug 06 '12 at 14:21
  • @assylias : i want to know if that is best way of doing it? – Rachel Aug 06 '12 at 14:23
  • The best way depends on the context as the answers show: if you need to control the formatting, you can use String.format, if you don't care, using `+` as you do is totally fine, if you do it repeatedly many times, a StringBuilder could make sense, etc. – assylias Aug 06 '12 at 14:43

7 Answers7

8

Java string concatenation is done by default with the + sign.

But there are times when this is not a good practice since this will create a new string instance every time you do it, so if you are in a loop this will create a new string in memory for each iteration.

So for simple cases, the + is perfectly valid.

For other cases, you should use StringBuilder.

Tomer
  • 17,787
  • 15
  • 78
  • 137
  • Agree w/ this answer. Can also use StringBuffer in other cases though (http://stackoverflow.com/questions/2971315/string-stringbuffer-and-stringbuilder) – Chandrew Aug 06 '12 at 14:33
4

I would perhaps use String.format() and you'll be able to control the formatting of the double in the string. e.g. you can control leading zeros, number of dps displayed etc.

See this SO answer for examples. And here's the doc for the underlying Formatter, and the standard tutorial.

Community
  • 1
  • 1
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
1

You can use String.valueOf(price) to convert. So you'd have

Double price = 5.34;
String trade = MSFT;
String key = String.valueOf(price) + trade;
thegrinner
  • 11,546
  • 5
  • 41
  • 64
0

String key = Double.toString(price) + trade;

prashanth
  • 445
  • 5
  • 14
0

To format the string - http://docs.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html

An example of how to use it.

http://www.tech-recipes.com/rx/1326/java-decimal-format-to-easily-create-custom-output/

To output it, use the + operand. This creates another string if you're looking for performance. You can also use string builder - http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/StringBuilder.html.

Cloud
  • 25
  • 4
0

You can also convert your Double with a toString().

Double price = 5.34;
String trade = MSFT;
String key = Double.toString(price) + trade;

Here is the detail for the toSting() method: http://docs.oracle.com/javase/7/docs/api/java/lang/Double.html#toString(double)

But using a StringBuffer is more efficient.

Regards, Dekx.

Afshin Moazami
  • 2,092
  • 5
  • 33
  • 55
Dekx
  • 402
  • 2
  • 15
0

There is nothing wrong with String key = price + trade. It is much more readable than String.valueOf(price) + trade or using StringBuilder.

Steve Kuo
  • 61,876
  • 75
  • 195
  • 257