安全字符串到 BigDecimal 的转换 [英] Safe String to BigDecimal conversion
问题描述
我正在尝试从字符串中读取一些 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 类,但是当它通过 double 运行时 - 大量的精度丢失了.
I've found a DecimalFormatter class, however as it operates through double - huge amounts of precision are lost.
那么,我该怎么做?
推荐答案
查看 setParseBigDecimal
十进制格式.使用此设置器,parse
将为您返回一个 BigDecimal.
Check out setParseBigDecimal
in DecimalFormat. With this setter, parse
will return a BigDecimal for you.
这篇关于安全字符串到 BigDecimal 的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!