This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Help! Question on converting String to BigInteger  RSS feed

 
Ranch Hand
Posts: 387
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What sort of Strings does getLineItemPrice() return? "14.57"?
Do you realize BigInteger handles "integer values" -- no
decimal places. Try BigDecimal.
 
Melinda Savoy
Ranch Hand
Posts: 387
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 387
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks the help. I'll give it a try.

Regards.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!