Java:如何将双精度连接到字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11830057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: How to concatenate Double to String
提问by Rachel
How can i concatenate Double
to String
in java?
我如何在java中连接Double
到String
?
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?
现在我有String key = price + trade
,不确定这是否是正确的做法,有什么建议吗?
回答by Tomer
Java string concatenation is done by default with the +
sign.
Java 字符串连接默认使用+
符号完成。
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.
对于其他情况,您应该使用StringBuilder。
回答by Brian Agnew
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.
我可能会使用String.format()并且您将能够控制字符串中双精度的格式。例如,您可以控制前导零、显示的 dps 数量等。
See this SO answerfor examples. And here's the doc for the underlying Formatter, and the standard tutorial.
回答by thegrinner
You can use String.valueOf(price)
to convert. So you'd have
可以String.valueOf(price)
用来转换。所以你会有
Double price = 5.34;
String trade = MSFT;
String key = String.valueOf(price) + trade;
回答by prashanth
String key = Double.toString(price) + trade;
String key = Double.toString(price) + trade;
回答by Cloud
To format the string - http://docs.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html
格式化字符串 - 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/
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.
要输出它,请使用 + 操作数。如果您正在寻找性能,这将创建另一个字符串。您还可以使用字符串生成器 - http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/StringBuilder.html。
回答by Dekx
You can also convert your Double with a toString().
您还可以使用 toString() 转换 Double。
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)
以下是 toSting() 方法的详细信息:http://docs.oracle.com/javase/7/docs/api/java/lang/Double.html#toString(double )
But using a StringBuffer is more efficient.
但是使用 StringBuffer 效率更高。
Regards, Dekx.
问候,德克克斯。
回答by Steve Kuo
There is nothing wrong with String key = price + trade
. It is much more readable than String.valueOf(price) + trade
or using StringBuilder
.
没有任何问题String key = price + trade
。它比String.valueOf(price) + trade
或使用StringBuilder
.