Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of the Java BigDecimal class in 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.

like image 261
Radi Avatar asked May 19 '10 06:05

Radi


People also ask

What is BigDecimal in 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.

What is BigDecimal class in Java?

A BigDecimal consists of an arbitrary precision integer unscaled value and a 32-bit integer scale. If zero or positive, the scale is the number of digits to the right of the decimal point. If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale.

How many bytes is a BigDecimal in Java?

Hence BigDecimal is (8+4+4)=16 bytes + BigInteger .

What is the default value of BigDecimal in Java?

If you are using type BigDecimal, then its default value is null (it is object, not primitive type), so you get [1] automatically.

What is BigDecimal class in Java?

BigDecimal Class in Java. The BigDecimal class provides operations on double numbers for arithmetic, scale handling, rounding, comparison, format conversion and hashing. It can handle very large and very small floating point numbers with great precision but compensating with the time complexity a bit.

What is BigDecimal in C++?

It can handle very large and very small floating point numbers with great precision but compensating with the time complexity a bit. A BigDecimal consists of a random precision integer unscaled value and a 32-bit integer scale. If greater than or equal to zero, the scale is the number of digits to the right of the decimal point.

Why use BigDecimal instead of int?

This class has methods that provide analogs for the operations that you perform on primitive types. That is, you can do anything with a BigDecimal that you can with an int or float, it’s just that you must use method calls instead of operators. Also, since there’s more involved, the operations will be slower. You’re exchanging speed for accuracy.

When should we use BigDecimal class in Python?

The BigDecimal class should be used when we need accuracy and particular scale in our results. BigDecimal is similar to other wrapper classes having specific methods for addition, subtraction, multiplication, and division. This wrapper class operations are slightly slower compared to primitive types.


2 Answers

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

like image 141
Gigo Avatar answered Sep 18 '22 11:09

Gigo


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.

like image 40
Dean Harding Avatar answered Sep 20 '22 11:09

Dean Harding