4 Replies Latest reply: Apr 4, 2007 1:50 AM by EJP

# equivalent for decimal type

Basically I am converting C# code to java code. So...
There is a decimal type in C sharp language. The size of this type is 128 bits.Can anyone tell me what is an equivalent for this type in java. which type should I use.?
• ###### 1. Re: equivalent for decimal type
I believe that C#'s decimal is suitable for financial applications since it has a greater precision than other floating point types.
If you need the precision, use the java.math.BigDecimal class, if not, you can probably use Java's primitive double (or the java.lang.Double wrapper).
• ###### 2. Re: equivalent for decimal type
The binary representation of a Decimal value consists of a 1-bit sign, a 96-bit integer number, and a scaling factor used to divide the 96-bit integer and specify what portion of it is a decimal fraction. The scaling factor is implicitly the number 10, raised to an exponent ranging from 0 to 28. -- http://msdn2.microsoft.com/en-us/library/system.decimal.aspx

So you could implement your own version using 4 ints, using longs for intermediate values so you can carry, or by splitting the integer part as the low 48 bits of a pair of longs. Though unless you have a profiler output saying that you can't use BigDecimal, you're better off not re-implementing types from the standard libraries.

Pete
• ###### 3. Re: equivalent for decimal type
Can you u tell me how much memory is allocated for BigDecimal and Wrapper class Double and whether it varies based on value we assign to the BigDecimal and Double objects.?
• ###### 4. Re: equivalent for decimal type
Can you u tell me how much memory is allocated
for BigDecimal
Nobody can.
and Wrapper class Double
Nobody can.
and whether
it varies based on value we assign to the BigDecimal
Yes it does.
and Double objects
No it doesn't.