Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

converting from BigDecimal to BigInteger

Thread view: 
Jeremy Watts - 17 Feb 2007 17:44 GMT
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
Lothar Kimmeringer - 17 Feb 2007 17:57 GMT
> 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/api/java/math/BigDecimal.html
Signature

Lothar Kimmeringer                E-Mail: spamfang@kimmeringer.de
              PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

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

Jeremy Watts - 17 Feb 2007 18:10 GMT
> > 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
>
[quoted text clipped - 5 lines]
> Always remember: The answer is forty-two, there can only be wrong
>                  questions!
Patricia Shanahan - 17 Feb 2007 18:21 GMT
> 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
[quoted text clipped - 5 lines]
> '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
Lothar Kimmeringer - 17 Feb 2007 18:34 GMT
> 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
Signature

Lothar Kimmeringer                E-Mail: spamfang@kimmeringer.de
              PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.