aspose file tools*
The moose likes Beginning Java and the fly likes Help!  Question on converting String to BigInteger Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help!  Question on converting String to BigInteger" Watch "Help!  Question on converting String to BigInteger" New topic
Author

Help! Question on converting String to BigInteger

Melinda Savoy
Ranch Hand

Joined: Jun 21, 2005
Posts: 387

I am dealing with data from a mainframe where all the fields are String data types. However some of the fields have deciaml(currency) values that I need to be able to convert to a BIGINTEGER data type and since BigInteger has no constructor that utilizes int I am lost at getting this string to a BigInteger. Any help or direction would be appreciated.

Here is the method that I started with and its blowing up because the bolded values below contain decimal values that are a String data type.

public BigInteger getRequisitionTotal(){
BigInteger itemprice = new BigInteger(getLineItemPrice());
BigInteger reqtotal = new BigInteger(getQuantityOrdered());
BigInteger extensionTotal = itemprice.multiply(reqtotal);
return extensionTotal;
}

Thanks.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
What sort of Strings does getLineItemPrice() return? "14.57"?
Do you realize BigInteger handles "integer values" -- no
decimal places. Try BigDecimal.


There is no emoticon for what I am feeling!
Melinda Savoy
Ranch Hand

Joined: Jun 21, 2005
Posts: 387

You are correct that the String value is like "8.54" and that BigInteger does handle integers. I was trying to get the value ultimately to a cents value such as 854. I did not want the number rounded as I understood that BigDecimal would ultimately do.

Am I understanding that correctly?

Thanks for the reply.
[ October 18, 2005: Message edited by: Melinda Savoy ]
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
With simple strings like that, I would extract the dollar and cents substrings
directly and avoid floating point rounding worries. For example:

This assumes that your values won't overflow a 32-bit int, and your
code may need to consider cases like "12" and ".34" and "-4.33".
Melinda Savoy
Ranch Hand

Joined: Jun 21, 2005
Posts: 387

Thanks the help. I'll give it a try.

Regards.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
If you can safely assume that your values won't overflow 32 bits, then you shouldn't use BigInteger. BigInteger introduces a lot of overhead that is unnecessary if you can store your values as an int or long. You should be able to take the example above and combine the dollars and cents into a single int value.

Layne


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help! Question on converting String to BigInteger