问题标签 [bigdecimal]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
383 浏览

double - 关于 BigDecimal

我有一个 csv 文件,其中除了标题和尾记录之外,每个详细记录中都存在数量和数量字段。拖车记录有一个总费用值,即数量的总和乘以明细记录中的金额字段。我需要检查拖车总费用值是否等于我计算的金额和数量字段的值。我对所有这些计算都使用双精度数据类型。当我浏览时,我可以从下面的 Web 链接中了解到,在与小数点比较时,使用双数据类型可能会产生问题。建议使用 BigDecimal

http://epramono.blogspot.com/2005/01/double-vs-bigdecimal.html

如果我使用双数据类型,我会遇到问题吗?如何使用 BigDecimal 进行计算。我也不确定在 csv 文件中我会在小数点后得到多少位数。金额也可以有正值或负值。

在 .csv 文件中

H,ABC..... "D",....,"1","12.23" "D",.....,"3","-13.334" "D",.... ..,"2","12" T,csd,123,12.345

------------------ 验证时我有以下代码------------ --------

-----------------------在阅读CSV文件时,我有以下代码

0 投票
3 回答
2364 浏览

ruby - 在 Ruby 中覆盖 BigDecimal to_s 默认值

当我从数据库表中检索数据时,会填充一个数组。一些字段被定义为十进制和货币字段,并且在数组中它们表示为 BigDecimal。

我使用这些数组值来填充 CSV 文件,但问题是所有 BigDecimal 值默认以科学格式表示(这是 BigDecimal to_s 方法的默认行为)。我可以使用 to_s('F') 显示值,但如何覆盖默认值?

0 投票
2 回答
3276 浏览

java - 关于大十进制

我有一个 csv 文件,其中除了标题和尾记录之外,每个详细记录中都存在数量和数量字段。拖车记录有一个总费用值,即数量的总和乘以明细记录中的金额字段。我需要检查拖车总费用值是否等于我计算的金额和数量字段的值。我对所有这些计算都使用双精度数据类型

在 csv 文件中,金额字段显示为“10.12”或“10”或“10.0”或“10.456”或“10.4555”或“-10.12”。金额也可以有正值或负值。

在 .csv 文件中

哈,ABC……

"D",....,"1","12.23"

"D",.....,"3","-13.334"

"D",......,"2","12"

T,csd,123,12.345

------------------ 验证时我有以下代码------------ --------

-----------------------在阅读CSV文件时,我有以下代码

我已将变量 tmpChargeQuan、tmpChargeAmt、calChargeCount 声明为 double

当我在网上搜索时,我知道 double 可能会给财务计算带来问题,因此需要使用 BIGDECIMAL。但我想知道这种情况是否适用于我的计算。在我的情况下,金额值在小数点后最多可以有 5 位或 6 位数字“我可以使用双精度数据类型进行此计算吗?我正在使用它进行验证。如果我将上述代码与使用双精度的乘法一起使用会产生问题吗?

0 投票
3 回答
1435 浏览

java - Regarding BigDecimal

i do the below java print command for this double variable double test=58.15; When i do a System.out.println(test); and System.out.println(new Double(test).toString()); It prints as 58.15.

When i do a System.out.println(new BigDecimal(test)) I get the below value 58.14999999999999857891452847979962825775146484375

I am able to understand "test" double variable value is internally stored as 58.1499999. But when i do the below two System.out.println i am getting the output as 58.15 and not 58.1499999.

System.out.println(test);

System.out.println(new Double(test).toString());

It prints the output as 58.15 for the above two.

Is the above System.out.println statements are doing some rounding of the value 58.1499999 and printing it as 58.15?

0 投票
2 回答
1614 浏览

java - 如何在 Swing GUI 中使用 BigDecimal?

我在 Java SE 应用程序中使用 BigDecimal 来表示产品价格。

我应该使用什么摆动组件以允许用户输入只有两位小数的数字并将其绑定到 BigDecimal 变量/对象的属性。(在用户输入时检查)?

我一直在玩 JTextField、JFormattedTextField、NumberFormatter、MaskFormatter,但我无法解决。

这些组件是否有任何组合和配置可以做到这一点?还是我应该扩展 MaskFormatter、JTextField、...?

0 投票
2 回答
15955 浏览

java - Java BigDecimal 内存使用情况?

是否有估算 a 消耗的内存量的指南BigDecimal

寻找与这些准则类似的东西来估计String内存使用情况。

0 投票
2 回答
999 浏览

java - 欢迎就创建我自己的 Swing 组件提出建议

最近我问到哪个 Swing 组件最好绑定到 BigDecimal 变量(具有一些特定的编辑属性)。事实证明,没有一个标准的 Swing 组件完全适合我,我在那里找到的第三方 Swing 组件库也不适合我。所以我决定创建自己的 Swing 组件。

组件说明:

我想扩展JTextFieldJFormattedTextField,因此我的新组件可以轻松绑定到BigDecimal变量。

该组件将具有可自定义的比例长度属性。

行为:

绘制组件时,它仅显示小数点和右侧的刻度数字空格。

当组件获得焦点时,插入符号应位于小数点左侧。当用户键入数字(任何其他字符被忽略)时,它们出现在插入符号的左侧,只接受长度-比例数字,任何其他键入的数字都被忽略,因为整数部分已满。每当用户键入小数点时,插入符号就会移动到小数点的右侧。以下键入的数字显示在小数部分中,仅考虑刻度数字,因为小数部分已满,任何其他键入的数字都将被忽略。此外,千位分隔符应显示为用户键入的数字保留到小数点。

我还希望能够将该组件用作JTable中的单元格编辑器(无需对其进行两次编码)。

在组件上调用 getValue() 方法应该会产生表示刚刚输入的数字的 BigDecimal。


我从未创建过自己的 Swing 组件;我几乎没用过标准的。因此,我将不胜感激有关创建所描述组件的任何好的教程/信息/提示。是我到目前为止唯一得到的东西。

提前致谢。

0 投票
3 回答
7496 浏览

java - BigDecimal、除法和 MathContext - 非常奇怪的行为

CentOs 5.4,OpenJDK 运行时环境(内部版本 1.6.0-b09)

漏洞 ?

0 投票
2 回答
1101 浏览

ruby-on-rails - BigDecimal to_s 与 to_f 不匹配

BigDecimal 类被破坏了吗?似乎永远不会发生以下情况:请注意 a.to_f != a.to_s.to_f

a.class => BigDecimal

a.to_f => 18658.1072928

a.to_s => "10865.81072928"

b.class => BigDecimal

b.to_f => 10000.0

b.to_s => "10000.0"

(a - b).to_f => 865.81072928

a.to_f - b.to_f => 8658.1072928

关于可能出现什么问题的任何想法?我们在服务器上运行 ruby​​ 1.8.7p72,在本地机器上运行 1.8.7p173。

0 投票
5 回答
26910 浏览

c++ - What are the best (portable) cross-platform arbitrary-precision math libraries?

I’m looking for a good arbitrary precision math library in C or C++. Could you please give me some advices or suggestions?

The primary requirements:

  1. It must handle arbitrarily big integers—my primary interest is on integers. In case that you don’t know what the word arbitrarily big means, imagine something like 100000! (the factorial of 100000).

  2. The precision must not need to be specified during library initialization or object creation. The precision should only be constrained by the available resources of the system.

  3. It should utilize the full power of the platform, and should handle “small” numbers natively. That means on a 64-bit platform, calculating (2^33 + 2^32) should use the available 64-bit CPU instructions. The library should not calculate this in the same way as it does with (2^66 + 2^65) on the same platform.

  4. It must efficiently handle addition (+), subtraction (-), multiplication (*), integer division (/), remainder (%), power (**), increment (++), decrement (--), GCD, factorial, and other common integer arithmetic calculations. The ability to handle functions like square root and logarithm that do not produce integer results is a plus. The ability to handle symbolic computations is even better.

Here are what I found so far:

  1. Java's BigInteger and BigDecimal class: I have been using these so far. I have read the source code, but I don’t understand the math underneath. It may be based on theories and algorithms that I have never learnt.

  2. The built-in integer type or in core libraries of bc, Python, Ruby, Haskell, Lisp, Erlang, OCaml, PHP, some other languages: I have used some of these, but I have no idea which library they are using, or which kind of implementation they are using.

What I have already known:

  1. Using char for decimal digits and char* for decimal strings, and do calculations on the digits using a for-loop.

  2. Using int (or long int, or long long) as a basic “unit” and an array of that type as an arbitrary long integer, and do calculations on the elements using a for-loop.

  3. Using an integer type to store a decimal digit (or a few digits) as BCD (Binary-coded decimal).

  4. Booth’s multiplication algorithm.

What I don’t know:

  1. Printing the binary array mentioned above in decimal without using naive methods. An example of a naive method: (1) add the bits from the lowest to the highest: 1, 2, 4, 8, 16, 32, … (2) use a char*-string mentioned above to store the intermediate decimal results).

What I appreciate:

  1. Good comparisons on GMP, MPFR, decNumber (or other libraries that are good in your opinion).

  2. Good suggestions on books and articles that I should read. For example, an illustration with figures on how a non-naive binary-to-decimal conversion algorithm works would be good. The article “<strong>Binary to Decimal Conversion in Limited Precision” by Douglas W. Jones is an example of a good article.

  3. Any help in general.

Please do not answer this question if you think that using double (or long double, or long long double) can solve this problem easily. If you do think so, you don’t understand the issue in question.