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

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

Leave a Comment