Is Java's BigDecimal the closest data type corresponding to C#'s Decimal?
Asked Answered
B

3

17

According to the chart here, the equivalent data type in Java to C#'s Decimal is BigDecimal.

Is this really so? What's up with the "Big" preamble? There doesn't seem to be a "SmallDecimal" or "LittleDecimal" (let alone "MediumSizedDecimal") in Java.

I must say, though, that chart was the clearest thing I found on the subject; the other links here and here and here were about as clear to me as the Mississippi River after a torrential tempest.

Benuecongo answered 11/4, 2014 at 16:10 Comment(0)
E
26

Yep - that's the corresponding type.

Since you are using Java after C# - don't be too surprised to find little nuances like this - or be too upset when there is no easy way to do something that's "easy" to do C#. The first thing that comes to my mind is int & int? - in Java you just use int and Integer.

C# had the luxury of coming after Java so lots of (what I subjectively see as) bad decisions have been fixed/streamlined. Also, it helps that C# was designed by Andres Hejlsberg (who is arguably one of the best programming language designers alive) and is regularly "updated" unlike Java (you probably witnessed all things added to C# since 2000 - complete list)

Enterpriser answered 11/4, 2014 at 16:15 Comment(5)
Good point on the nullable int in Java being integer. I agree on Hejlsberg - he deserves a spot on the Mt. Rushmore of geeks. I still wonder why it's called "Big" decimal, though - big in relation to what?Benuecongo
@B.ClayShannon I can only speculate, but my guess is that it's named BigDecimal since it's for "Big Decimals". More here - java2s.com/Questions_And_Answers/Java-Data-Type/bigdecimal/…Enterpriser
There is a good reason why it is called out as "Big". See my answer for details.Cnidoblast
Now, as for Java being "regularly updated", I note that Java did just undergo a major revision and as a result it now has many of the features of C# 2.0.Cnidoblast
@EricLippert I hope they keep going with "updating" ;). I would love to see var in Java the way it's implemented in C# - it would help a lot with declaring instancing of all those Class.Subclass that are very common in Java (or at least in Android)Enterpriser
C
33

Is this really so?

They are similar but not identical. To be more specific: the Java version can represent every value that the C# version can, but the opposite is not true.

What's up with the "Big" preamble?

A Java BigDecimal can have arbitrarily much precision and therefore can be arbitrarily large. If you want to make a BigDecimal with a thousand places of precision, you go right ahead.

By contrast, a C# decimal has a fixed size; it takes up 128 bits and gives you 28 decimal places of precision.

To be more precise: both types give you numbers of the form

+/- someInteger / 10 ^ someExponent

In C#, someInteger is a 96 bit unsigned integer and someExponent is an integer between 0 and 28.

In Java, someInteger is of arbitrary size and someExponent is a signed 32 bit integer.

Cnidoblast answered 11/4, 2014 at 19:42 Comment(3)
Another major difference is that by default, BigDouble will either perform math exactly or throw an exception when it cannot. By contrast, Decimal will silently round off decimal digits which it cannot handle. For example, when dividing 1 into 536870912, Java's BigDecimal will compute the value as 0.00000000186264514923095703125 while .NET's Decimal will compute it as 0.0000000018626451492309570312D.Dwelt
@Dwelt BigDouble? where did that come from? did you mean BigDecimal?Hectometer
@MauricioMorales: Mea culpa. Yeah, BigDecimal.Dwelt
E
26

Yep - that's the corresponding type.

Since you are using Java after C# - don't be too surprised to find little nuances like this - or be too upset when there is no easy way to do something that's "easy" to do C#. The first thing that comes to my mind is int & int? - in Java you just use int and Integer.

C# had the luxury of coming after Java so lots of (what I subjectively see as) bad decisions have been fixed/streamlined. Also, it helps that C# was designed by Andres Hejlsberg (who is arguably one of the best programming language designers alive) and is regularly "updated" unlike Java (you probably witnessed all things added to C# since 2000 - complete list)

Enterpriser answered 11/4, 2014 at 16:15 Comment(5)
Good point on the nullable int in Java being integer. I agree on Hejlsberg - he deserves a spot on the Mt. Rushmore of geeks. I still wonder why it's called "Big" decimal, though - big in relation to what?Benuecongo
@B.ClayShannon I can only speculate, but my guess is that it's named BigDecimal since it's for "Big Decimals". More here - java2s.com/Questions_And_Answers/Java-Data-Type/bigdecimal/…Enterpriser
There is a good reason why it is called out as "Big". See my answer for details.Cnidoblast
Now, as for Java being "regularly updated", I note that Java did just undergo a major revision and as a result it now has many of the features of C# 2.0.Cnidoblast
@EricLippert I hope they keep going with "updating" ;). I would love to see var in Java the way it's implemented in C# - it would help a lot with declaring instancing of all those Class.Subclass that are very common in Java (or at least in Android)Enterpriser
B
1

The C# Decimal and java BigDecimal types are not equivalents. BigDecimal is arbitrary precision. It can literally represent any number to any precision (until you run out of RAM).

C# Decimal is floating point but "fixed length" (128 bits). Most of the time, that's enough! Decimal is much faster than BigDecimal, so unless you really need a lot of precision it is a superior option.

What you probably want for Java is https://github.com/tools4j/decimal4j or a similar library.

Belle answered 30/9, 2022 at 17:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.