What is the equivalent of the Java BigDecimal class in C#?

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:

PrecisionSpec precision = new PrecisionSpec(1024, PrecisionSpec.BaseType.BIN);
BigFloat bf = new BigFloat(13, precision);
bf.Sqrt();
Console.WriteLine(bf.ToString());

Wikipedia has a list of other such libraries at http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic#Libraries

Sources:

  • The BigNum library was originally hosted at http://www.fractal-landscapes.co.uk/bigint.html, but that site has been down since 2012. (And then came back up at some point).
  • You can find an archive of the site at http://web.archive.org/web/20110721173046/http://www.fractal-landscapes.co.uk/bigint.html.
  • There's a copy of the source code at http://www.mediafire.com/file/6axoicc6iszp4sg/BigNum.zip/file

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.


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


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.