C# 中的 Java BigDecimal 类相当于什么?
BigDecimal 是 java.math 包中的一个类,它对于处理一定规模的大数有很多好处。 C# 中是否有具有此功能的等效类或数据类型。
BigDecimal
is a class in the java.math
package that has a lot of benefits for handling big numbers of a certain scale. Is there an equivalent class or data type in c# with this feature.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您还可以使用 Math.Gmp.Native NuGet 包我写。其源代码可在 GitHub 上获取,文档可在 此处。它向 .NET 公开 GMP 库的所有功能,该库被称为高度优化的任意精度算术图书馆。
任意精度浮点数由 mpf_t 类型。对这些浮点数的运算均以 mpf_ 前缀开头。例如, mpf_add或 mpf_cmp。每个操作都给出了源代码示例。
You can also use the Math.Gmp.Native NuGet package that I wrote. Its source code is available on GitHub, and documentation is available here. It exposes to .NET all of the functionality of the GMP library which is known as a highly-optimized arbitrary-precision arithmetic library.
Arbitrary-precision floating-point numbers are represented by the mpf_t type. Operations on these floating-point numbers all begin with the
mpf_
prefix. For examples, mpf_add or mpf_cmp. Source code examples are given for each operation.就在最近,我还需要 C# 中的任意精度小数,并发现了这里发布的想法: https://stackoverflow.com/a/4524254 /804614
然后我完成了草案,以支持所有基本算术和比较运算符,以及所有典型数值类型和一些指数方法之间的转换,这是我当时需要的。
它当然不全面,但非常实用,而且几乎随时可用。由于这是一晚编码的结果,我不能保证这个东西没有错误或完全准确,但它对我来说非常有用。无论如何,我想在这里发布它,因为我没有找到任何其他方法可以在 C# 中使用任意精度小数,而无需包含大量库(大多数甚至不是 .net,而是 C++ 的包装器),这些库附带了各种不必要的功能东西。
基本思想是使用 .NET 4.0 的 BigInteger 类型和以 10 为底的指数 (Int32) 构建具有任意大尾数的自定义浮点类型。
如果您发现错误/不准确之处,有建议或任何有建设性的内容,请随时直接编辑我的帖子或发表评论,以便我可以改进答案。
我不完全确定这是否是放置这个东西的最佳位置,但这是关于这个主题的首要问题之一,我真的很想分享我的解决方案。 ;)
编辑:我将实现移至 GitHubGist: https://gist.github.com/JcBernack/0b4eef59ca97ee931a2f45542b9ff06d< /a>
Just recently I also needed an arbitrary precision decimal in C# and came across the idea posted here: https://stackoverflow.com/a/4524254/804614
I then completed the draft to support all basic arithmetic and comparison operators, as well as conversions to and from all typical numerical types and a few exponential methods, which I needed at that time.
It certainly is not comprehensive, but very functional and almost ready-to-use. As this is the result of one night coding, I can not assure that this thing is bug free or entirely exact, but it worked great for me. Anyway, I want to publish it here because I did not find any other way to use arbitrary precision decimals in C# without the need to include massive librarys (mostly not even .net, but wrappers to c++), which come with all kinds of unnecessary stuff.
The basic idea is to build a custom floating-point type with an arbitrary large mantissa using the BigInteger type of .NET 4.0 and a base 10 exponent (Int32).
If you find bugs/inaccuracies, have suggestions or anything constructive, please feel free to directly edit my post or leave a comment so I may improve the answer.
I'm not entirely sure if this is the best spot to place this thing, but this is one of the top questions on SO about this topic and I really want to share my solution. ;)
EDIT: I moved the implementation to GitHubGist: https://gist.github.com/JcBernack/0b4eef59ca97ee931a2f45542b9ff06d
C# 仅使用
BigInteger
构建了它(在 .NET Framework 4 中)。decimal
的精度足以满足您的任务吗?它是一个 128 位数字,可以保存 ±1.0 × 10−28 到 ±7.9 × 1028 范围内的值。C# only has
BigInteger
built it (in .NET framework 4).Is
decimal
enough precision for your task? It's a 128-bit number that can hold values in the range ±1.0 × 10−28 to ±7.9 × 1028.那么,除了使用支持 BigDecimal 的第三方库(如果存在)之外,没有简单的解决方法。就我而言,最简单的方法是采用十进制实现(例如来自 mono)并使用 BigInteger 类型重写它。在内部,在 mono 的实现中,十进制类型由三个整数组成。所以我认为这不会很难实施。但我不确定效率。然而,您应该首先考虑使用标准十进制类型,如 codeka 提到的。
Well, apart from using third-party libraries with support of the BigDecimal (if they exist), there are no easy workarounds. The most easy way, as far as i am concerned is to take a decimal implementation( from mono for example) and to rewrite it using the BigInteger type. Internally, in mono's implementation, decimal type is composed from three integers. So i don't think that would be hard to implement. I am not sure about efficiency though. You should first however consider using standard decimal type as codeka mentioned.
有一个名为 BigNum 的 C# 库可以满足您的需求,并且在某些情况下还具有其他功能。
例如,它有一个平方根函数,而 BigDecimal 没有:
维基百科在 http://en.wikipedia.org/wiki/Arbitrary- precision_arithmetic#Libraries
来源:
There's a C# library called BigNum that does what you're looking for, and in some cases has additional functionality.
For example, it has a square root function, which BigDecimal doesn't have:
Wikipedia has a list of other such libraries at http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic#Libraries
Sources:
Deveel Math
GitHub:
https://github.com/deveel/deveel-math
作者 GitHub:
< a href="https://github.com/tsutomi" rel="nofollow">Antonello Provenzano
支持:
如何安装
从 NuGet 包管理控制台,选择其中的项目将安装该库并键入以下命令
Deveel Math
GitHub:
https://github.com/deveel/deveel-math
Author GitHub:
Antonello Provenzano
Support:
How to Install It
From the NuGet Package Management console, select the project where the library will be installed and type the following command
当问题最初发布时,这可能不是一个选项,但在 C# 代码中使用 BigDecimal 的一种非常简单的方法是安装 IKVM.NET 通过 NuGet 进行打包:
然后按照在 Java 中的方式进行操作:
根据您使用 IKVM 的程度,可能会偶尔出现互操作问题偶然发现,但根据我的经验,它通常对于像这样的简单事情非常有效。
This may not have been an option when the question was originally posted, but one really easy way to use a
BigDecimal
in your C# code is to install the IKVM.NET package via NuGet:Then do exactly as you would in Java:
Depending on how far you go with IKVM there can be the occasional interop issue to stumble through but in my experience it usually works great for simple stuff like this.