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

前端 未结 2 1458
温柔的废话
温柔的废话 2020-12-17 15:51

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\

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-17 16:38

    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.

提交回复
热议问题