如何将字符串数字解析为BigDecimal [英] How to parse a string number into a BigDecimal
问题描述
可能重复:
安全字符串到BigDecimal的转换
Possible Duplicate:
Safe String to BigDecimal conversion
java中是否有任何最终解析函数可以分析字符串中给定数字的类型,并在Number类或类似类的对象中返回其值.
我想将字符串转换为bigDecimal或Number并将t放入集合中,有时我会使用2.23
或2,23
或2
或BigInteger这样的字符串,例如233242424242424242
.
Is there any ultimate parse function in java which can analyze type of the number given in string and return its value in object of class Number or similar.
I would like to convert string into bigDecimal or Number and put t into collection, sometimes I have string like 2.23
or 2,23
or 2
or BigInteger like 233242424242424242
.
推荐答案
如果只想将String
解析为BigDecimal
,则可以使用
If all you want is to parse a String
to a BigDecimal
, you can use the constructor that gets a string as a argument. The constructor takes care of the basic parsing.
BigDecimal b = new BigDecimal(str);
对于复杂的解析(涉及数千个分隔符),可以使用 DecimalFormat
类和
For complex parsing (involving thousands separators), you can use the methods in the DecimalFormat
class with setParseBigDecimal
set to true
.
这篇关于如何将字符串数字解析为BigDecimal的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!