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
posted
0
What sort of Strings does getLineItemPrice() return? "14.57"? Do you realize BigInteger handles "integer values" -- no decimal places. Try BigDecimal.
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
posted
0
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".
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.