Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > converting from BigDecimal to BigInteger

Reply
Thread Tools

converting from BigDecimal to BigInteger

 
 
Jeremy Watts
Guest
Posts: n/a
 
      02-17-2007
I am trying to convert a BigDecimal number to a BigInteger one. I have
truncated the number to its nearest whole integer firstly, before converting
it to a string and then converting that to a BigInteger.... but the
compilers coming back with an error message "Number Format Exception".

Cant see what I'm doing wrong here... The number I'm using for the
BigDecimal is
'465836768828.000' . Is it the ".000" bit thats messing things up here?
How would I remove that so that I'm left with 465836768828 ?

Or is there a better way of converting between the two?

Thanks



 
Reply With Quote
 
 
 
 
Lothar Kimmeringer
Guest
Posts: n/a
 
      02-17-2007
Jeremy Watts wrote:

> Or is there a better way of converting between the two?


RTFM[1]

BigInteger bi = mybigdecimal.toBigInteger();


Regards, Lothar

[1] http://java.sun.com/j2se/1.3/docs/ap...igDecimal.html
--
Lothar Kimmeringer E-Mail: http://www.velocityreviews.com/forums/(E-Mail Removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
Reply With Quote
 
 
 
 
Jeremy Watts
Guest
Posts: n/a
 
      02-17-2007

"Lothar Kimmeringer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Jeremy Watts wrote:
>
> > Or is there a better way of converting between the two?

>
> RTFM[1]
>
> BigInteger bi = mybigdecimal.toBigInteger();


hee hee that simple eh? I'm sure I'd done this before somewhere
without converting to strings first... anyway thanks

>
>
> Regards, Lothar
>
> [1] http://java.sun.com/j2se/1.3/docs/ap...igDecimal.html
> --
> Lothar Kimmeringer E-Mail: (E-Mail Removed)
> PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)
>
> Always remember: The answer is forty-two, there can only be wrong
> questions!



 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      02-17-2007
Jeremy Watts wrote:
> I am trying to convert a BigDecimal number to a BigInteger one. I have
> truncated the number to its nearest whole integer firstly, before converting
> it to a string and then converting that to a BigInteger.... but the
> compilers coming back with an error message "Number Format Exception".
>
> Cant see what I'm doing wrong here... The number I'm using for the
> BigDecimal is
> '465836768828.000' . Is it the ".000" bit thats messing things up here?
> How would I remove that so that I'm left with 465836768828 ?


Yes, the "." is not a permitted character: "The String representation
consists of an optional minus sign followed by a sequence of one or more
decimal digits."

>
> Or is there a better way of converting between the two?


I half agree with the toBigInteger suggestion. If you expect your
BigDecimal to always be an integer, you could use toBigIntegerExact,
which throws an exception on data loss.

Patricia
 
Reply With Quote
 
Lothar Kimmeringer
Guest
Posts: n/a
 
      02-17-2007
Patricia Shanahan wrote:

> I half agree with the toBigInteger suggestion. If you expect your
> BigDecimal to always be an integer, you could use toBigIntegerExact,
> which throws an exception on data loss.


He didn't mention the Version of Java the program should work.
toBigIntegerExact exists since 1.5.


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (E-Mail Removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
BigDecimal(String) vs. BigDecimal(double) [Floating-point arithmetics] Stanimir Stamenkov Java 4 07-18-2008 10:49 AM
BigDecimal to power of BigDecimal czarnysfetr@gmail.com Java 3 02-05-2007 02:49 PM
converting BigDecimal to BigInteger Jeremy Watts Java 2 06-28-2005 09:24 AM
how to convert "BigInteger" into "Integer"? how to print out a BigInteger binary value? nick Java 1 10-26-2004 02:45 PM
how to convert "BigInteger" into "Integer"? how to print out a BigInteger binary value? nick Java 0 10-26-2004 08:18 AM



Advertisments