问题描述
我正在尝试从字符串中读取一些BigDecimal值。假设我有这个字符串:1,000,000,000.999999999999999,我希望得到一个BigDecimal。有什么办法呢?
I'm trying to read some BigDecimal values from the string. Let's say I have this String: "1,000,000,000.999999999999999" and I want to get a BigDecimal out of it. What is the way to do it?
首先,我不喜欢使用字符串替换的解决方案(替换逗号等)。我认为应该有一些整洁的格式化程序来为我完成这项工作。
First of all, I don't like the solutions using string replaces (replacing commas etc.). I think there should be some neat formatter to do that job for me.
我找到了一个DecimalFormatter类,但是因为它通过双重操作 - 大量的精度是丢失了。
I've found a DecimalFormatter class, however as it operates through double - huge amounts of precision are lost.
那我该怎么办呢?
推荐答案
查看。使用此setter,将为您返回一个BigDecimal。
Check out setParseBigDecimal
in DecimalFormat. With this setter, parse
will return a BigDecimal for you.
这篇关于安全字符串到BigDecimal转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!