Java精确计算 - 使用选项 [英] Java precise calculations - options to use

查看:106
本文介绍了Java精确计算 - 使用选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力建立一些简明扼要的概述,我们在JAVA + SQL中有什么选项用于精确的测量。到目前为止,我已经找到以下选项:

I am trying to establish some concise overview of what options for precise caluclations we have in JAVA+SQL. So far I have found following options:


  1. 使用双打接受他们的缺点,不要去。

  2. 使用BigDecimals

    • 在复杂的公式中使用它们对我来说是有问题的


  • 我需要在公式中舍入每个变量,或者只是结果来获得BigDecimal精度?

  • 如何调整?


  • 缺点是我需要动态SQL从不同的表中提取数据+计算其他计算字段的字段,并且会变得混乱

任何其他选项?

问题陈述:
我需要精确的财务计算,这将涉及使用非常大(数十亿)和非常小的数字(0.0000004321),并且分割彼此非常相似的值,所以确定我需要BigDecimal的精度。

Problem statement: I need precise financial calculations that would involve using very big (billions) and very small numbers (0.0000004321), and also dividing values that are very similar to each other, so for sure I need precision of BigDecimal.

另一方面,我想保留功能中的双重使用(我从十进制SQL数据中的数组上工作),所以计算如下:(a [i] - b [ i])/ b [i]等,进一步用于其他计算。我想让用户能够根据需要使用自己的公式(使用常见的数学语句)

On the other side, I want to retain ease of use that doubles have in functions (i work on arrays from decimal SQL data), so calculations like: (a[i] - b[i])/b[i] etc. etc. that are further used in other calculations. and I'd like to have users to be able to desing their own formulas as they need them (using common math statements)

我很想使用格式化解决方案对于String.format,但是这使得代码不是很可读(对于每个变量使用String.format())。

i am keen to use "formatting" solution for String.format, but this makes code not very readable ( using String.format() for each variable...).

非常感谢建议如何处理与东西。

Many thanks for suggestion of how to deal with the stuff.

推荐答案

没有办法得到 BigDecimal C $ C>双。 double s具有双重精度。

There is no way to get BigDecimal precision on a double. doubles have double precision.

如果要保证精确的结果,请使用 BigDecimal

If you want to guarantee precise results use BigDecimal.

您可以使用 long 存储整数部分和 int 以存储小数部分 - 但是为什么要重新发明轮。

You could create your own variant using a long to store the integer part and an int to store the fractional part - but why reinvent the wheel.

任何时间使用 double 您可以从双重精度问题来填充。如果您在一个地方使用它们,您可以随时使用它们。

Any time use doubles you stand to stuffer from double precision issues. If you use them in a single place you might as well use them everywhere.

即使您只使用它们来表示数据库中的数据,则将数据舍入到双精准,您将失去信息。

Even if you only use them to represent data from the database then will round the data to double precision and you will lose information.

这篇关于Java精确计算 - 使用选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆