tags:

views:

1264

answers:

5

hi can any tell me how to cast Long to BigDecimal

thanks In Advance

+3  A: 

You can't cast it. You can create a new BigDecimal though. You can get a long from a Long using Long.getLongValue() if you have the non-primitave Long.

BigDecimal bigD = new BigDecimal(longVal);
jjnguy
Darn, too slow.
jjnguy
+6  A: 

You'll have to create a new BigDecimal.

BigDecimal d = new BigDecimal(long);
Silfverstrom
A: 

you have to create a new bigDecimal

how to do it

Fredou
+2  A: 

You need to create a new BigDecimal object

  Long test = new Long (10);
  BigDecimal bigD = new BigDecimal(test.longValue());
Mike Pone
+3  A: 

For completeness you can use:

// valueOf will return cached instances for values zero through to ten
BigDecimal d = BigDecimal.valueOf(yourLong);

0 - 10 is as of the java 6 implementation, not sure about previous JDK's

Gareth Davis
valueOf is preferred as per the JavaDocs: 'This "static factory method" is provided in preference to a (long) constructor because it allows for reuse of frequently used BigDecimal values.'
Steve Kuo