Java 将 BigDecimal 转换为 String 的正确方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20700841/
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
Right way to convert of BigDecimal to String
提问by cbheema
Why I should use valueOf()
?
why cant I use the implicit conversion?
为什么我应该使用valueOf()
?为什么我不能使用隐式转换?
Code Snippet is shown below:
代码片段如下所示:
BigDecimal xmlvalue = new BigDecimal(12.3434379328948927894789237);
String str1 = String.valueOf(xmlvalue); //Option 1
String str2 = "" + xmlvalue; //Option 2
回答by anubhava
You can use BigDecimal#toString
to convert a BigDecimal to string
您可以使用BigDecimal#toString
将 BigDecimal 转换为字符串
回答by grexter89
you can use the toString()
method
你可以使用这个toString()
方法
xmlvalue.toString();
回答by Philipp Cla?en
One could argue that it is more readable to use valueOf(x)
than "" + x
. Internally, both will fall back to toString
but they can also deal with null values (where null.toString()
will throw a NullPointerException
).
有人可能会争辩说,它valueOf(x)
比"" + x
. 在内部,两者都会回退到toString
但它们也可以处理空值( wherenull.toString()
将抛出 a NullPointerException
)。
In theory, the performance of valueOf(x)
should be better than "" + x
, as the latter uses a StringBuilder
internally, which can lead to some overhead.
理论上, 的性能valueOf(x)
应该优于"" + x
,因为后者在StringBuilder
内部使用了,这会导致一些开销。
When you know that your BigDecimal
is non-null (as in your example), you should just use toString
.
当您知道您BigDecimal
的非空值(如您的示例中所示)时,您应该只使用toString
.
By the way, be careful with the BigDecimal(double)
constructor. The safer approach is to use
new BigDecimal("12.3434379328948927894789237")
in your example. The reason why BigDecimal(double)
is unpredicable is explained in its Javadoc comment.
顺便说一句,要小心BigDecimal(double)
构造函数。更安全的方法是new BigDecimal("12.3434379328948927894789237")
在您的示例中使用
。BigDecimal(double)
不可预测的原因在其 Javadoc 注释中进行了解释。
回答by helpermethod
For two reasons:
有两个原因:
- Using
String.valueOf
better conveys your intent: you obviously try to get the String representation of a BigDecimal value; abusing String concatenation requires a little more mental effort to understand what you are trying to do - It may be faster: Using concatenation will internally create a StringBuilder, which concatenates two Strings and then calls toString() onto itself (though the compiler may be clever enough to get rid of these unnecessary operations)
- 使用
String.valueOf
更好地传达了您的意图:您显然试图获得 BigDecimal 值的字符串表示;滥用字符串连接需要更多的脑力来理解你想要做什么 - 可能会更快:使用串联将在内部创建一个 StringBuilder,它串联两个字符串,然后对其自身调用 toString()(尽管编译器可能足够聪明以摆脱这些不必要的操作)
回答by Dropout
Why would you ever manually use "" + string
for a conversion to String? I know that a lot of developers use that, but to me it seems just like taking an advantage of Java's developer friendliness and the fact that it does a lot of stuff(terminus technicus "conversions") automatically. In other words it's a lazy way around and I don't like raping Java when String.valueOf()
is implemented exactly for this purpose.
你为什么要手动使用"" + string
来转换为字符串?我知道很多开发人员都在使用它,但对我来说,这似乎只是利用了 Java 的开发人员友好性以及它自动完成很多事情(terminus technicus“转换”)的事实。换句话说,这是一种懒惰的方法,我不喜欢在String.valueOf()
完全为此目的实现Java 时强袭Java 。
Technical answer:
技术解答:
When a + ""
is used with an integer a
. It behaves as such:
当a + ""
与整数一起使用时a
。它的行为如下:
String.valueOf(a) + new String("");
This creates two String objects, while String.valueOf(a)
uses just one.
这将创建两个 String 对象,而String.valueOf(a)
只使用一个。
回答by herry
The String#valueOf use Double#toStringmethod in this case.
在这种情况下, String#valueOf 使用Double#toString方法。
I think is better use BigDecimal#toStringmethod. For example if you want to converted back this data, it better use this toString method.
我认为最好使用BigDecimal#toString方法。例如,如果您想将这些数据转换回来,最好使用这个 toString 方法。
If that string representation is converted back to a BigDecimal using the BigDecimal(String) constructor, then the original value will be recovered.
如果使用 BigDecimal(String) 构造函数将该字符串表示形式转换回 BigDecimal,则将恢复原始值。
回答by Peter Lawrey
Using BigDecimal doesn't bring back the precision lost in double
If you need more than 15 digits of accuracy you need to use String and BigDecimal.
使用 BigDecimal 不会恢复丢失的精度double
如果您需要超过 15 位的精度,则需要使用 String 和 BigDecimal。
BigDecimal xmlvalue = new BigDecimal("12.3434379328948927894789237");
String str1 = xmlvalue.toString(); // Simplest Option
If you have a double
you may as well just do
如果你有一个double
你也可以这样做
// creating BigDecimal here doesn't help unless you do rounding.
String str1 = String.valueOf(d);
回答by Sabir Khan
I think better way is to use class - java.text.DecimalFormat
because in practical scenarios , there is no relation between actual value represented by these number classes like BigDecimal
& their String representations, though you haven't listed your reason for wanting a String value for an altogether different type of data.
我认为更好的方法是使用类 -java.text.DecimalFormat
因为在实际场景中,这些数字类表示的实际值之间没有关系,例如BigDecimal
& 它们的 String 表示,尽管您没有列出想要完全不同类型的 String 值的原因数据的。
In my opinion, String representation is all about formatting & this DecimalFormat
class gives you better control over that e.g. I needed to write few BigDecimal
values to a text file and different values needed to be in different format.
在我看来,字符串表示完全是关于格式化的,这个DecimalFormat
类可以让你更好地控制它,例如我需要将几个BigDecimal
值写入文本文件,而不同的值需要采用不同的格式。
A portion of Java Doc,
Java Doc的一部分,
DecimalFormat is a concrete subclass of NumberFormat that formats decimal numbers. It has a variety of features designed to make it possible to parse and format numbers in any locale, including support for Western, Arabic, and Indic digits. It also supports different kinds of numbers, including integers (123), fixed-point numbers (123.4), scientific notation (1.23E4), percentages (12%), and currency amounts ($123). All of these can be localized.
DecimalFormat 是 NumberFormat 的具体子类,用于格式化十进制数。它具有多种功能,旨在使解析和格式化任何语言环境中的数字成为可能,包括对西方、阿拉伯和印度数字的支持。它还支持不同种类的数字,包括整数 (123)、定点数 (123.4)、科学记数法 (1.23E4)、百分比 (12%) 和货币金额 ($123)。所有这些都可以本地化。