任意预设精度小数(几乎类似于 BigDecimal) [英] Arbitrary pre-set precision decimals (Almost like BigDecimal)
问题描述
我正在寻找一个 Java 类,它可以保存一个具有任意预设精度的十进制值.类 BigDecimal
看起来是一个很好的起点,但我希望能够将小数的大小限制为最终用户决定的任何内容.从字面上看,最终用户会指定一个以 10 为基数的范围,我会分配足够的字节来表示该范围内的任何数字.
I'm looking to have a java class that will hold a decimal value with an arbitrary pre-set precision. The class BigDecimal
looks to be a good candidate as a starting point, but I want to able to limit the size of the decimal to whatever an end user decides. Literally speaking, the end user would specify a range in base 10 and I would allocate enough bytes to represent any number in that range.
我想拥有一个扩展 BigDecimal
的类.在本课程中,我将添加在预设范围内模拟小数所需的功能.
I was thinking of having a class that extends BigDecimal
. In this class I would add the functions necessary to mimic a decimal within a pre-set range.
我向社区提出的问题是:是否有任何图书馆已经这样做了?
扩展 BigDecimal
是否合理?
My questions to the community are:
Are there any libraries that already do this?
Would extending BigDecimal
be a reasonable thing to do?
推荐答案
我建议你在你提出的建议之间采取一种中间方式:
I would suggest you an intermediate way in between the proposals you made:
Apfloat
库是一个很好的尝试方式,请查看 API 文档.Apcomplex
是一个包装类,例如一个 BigDecimal
.
The Apfloat
library is a good way to try with, check out the API docs.
Apcomplex
is a wrapper class for e.g. a BigDecimal
.
因此,在这里,您将有很大的自由来覆盖和增强仅应用此类的新方法.
So here you would have a big freedom on overriding and enhancing new methods just applying this class.
这篇关于任意预设精度小数(几乎类似于 BigDecimal)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!