Java 中的边界案例测试
是否有适用于 JUnit 或 Java 实践的工具可以准确测试“小于”和“大于”等边界情况测试?
例如,将货币价值四舍五入到最接近的美分。 测试 0.005 舍入到 0.01 很容易。 测试<0.005 向下舍入不是(严格来说)。
显然,我可以测试像 0.0049 这样的任意值,但这并不能保证像 0.00499 这样的另一个值不会被四舍五入。是否有一个常量或其他任何东西可以保证数值最接近于正在执行的环境的“小于”或“大于”?
Are there any tools for JUnit or Java practises that allow for boundary case tests like "less than" and "greater than" to be accurately tested?
For example, rounding monetary values half-up to nearest cent.
Testing 0.005 rounds up to 0.01 is easy.
Testing <0.005 rounds down is not (strictly speaking).
I can obviously test an arbitrary value like 0.0049, but this doesn't guarantee that another value like 0.00499 doesn't get rounded up. Is there a constant or whatever that guarantees the numerical value that is the closest possible representation to "less than" or "greater than" for the environment being executed on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Math.nextAfter(0.005, 0)
?http:// download.oracle.com/javase/6/docs/api/java/lang/Math.html#nextAfter%28double,%20double%29
Math.nextAfter (0.005, 0)
?http://download.oracle.com/javase/6/docs/api/java/lang/Math.html#nextAfter%28double,%20double%29
可能,但这取决于很多因素。对于货币值,您应该始终使用 BigDecimal,因为它可以让您完全控制精度(例如 double 或 float 可以给出偶数结果)对于简单值)
Probably but this depends on many factors. For monetary values, you should always use
BigDecimal
since it gives you complete control over the precision (likedouble
orfloat
which can give odd results even for simple values)