Java中最高精度的测试编号 [英] Test Number For Maximum Precision In Java
本文介绍了Java中最高精度的测试编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想测试双精度的最大精度为3或更小.用Java做到这一点的最佳方法是什么?
I would like to test a double for a maximum percision of 3 or less. What is the best way to do this in Java?
20.44567567 <- Fail
20.444 <- Pass
20.1 <- Pass
20 <- Pass
推荐答案
1)不要使用double
.浮点逻辑充其量是近似的.改为使用BigDecimal
.
1) Do not use double
. Floating point logic is approximated at best. Use BigDecimal
instead.
2)我认为BigDecimal
已经有一种设置精度的方法.如果不是,则乘以1000并截断.进行操作,获取一个新的数字,然后与原始数字进行比较.如果不同,则失败.
2) I think BigDecimal
already has a way of setting a precision. If not, just multiply by 1000 and trunc. Do the operation, get a new number, and compare to the original one. If it is different, fail.
这篇关于Java中最高精度的测试编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文