如何将字符串数字解析为BigDecimal [英] How to parse a string number into a BigDecimal

查看:348
本文介绍了如何将字符串数字解析为BigDecimal的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
安全字符串到BigDecimal的转换

Possible Duplicate:
Safe String to BigDecimal conversion

java中是否有任何最终解析函数可以分析字符串中给定数字的类型,并在Number类或类似类的对象中返回其值. 我想将字符串转换为bigDecimal或Number并将t放入集合中,有时我会使用2.232,232或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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆