This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

conversion of String object to integer primitive  RSS feed

 
Ranch Hand
Posts: 37
Java Spring Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

i am having a field called "Opening Account Balance" in the JSP and i am using a JavaScript file to change its behavior.
suppose i enter 245 it will be changed to $245.00
suppose i enter 2453 it will be changed to $2,453.00
suppose i enter 24534 it will be changed to $ 24,534.00


Now i want that amount what i entered in the same form in to a java file. i was able to get the amount but in a string format. now when i am applying the Integer.parseInt(obj) its giving NumberFormatException.

Because its unable to convert the $ and , characters. for that i need to write a function which will delete the "," and "$" from the string and then i need to apply Integer.parseInt(obj).

My Question: is there any built-in function in java that will convert $24,54,980.00 to 2454980.

regards,
Vijaya Raghava.
 
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

The requirement is ambiguous.

If you have 2345 number then you can easily convert it into $2345.

as,

int value=2345;
String k="$"+value;

But in your requirement you want to put , to seperate the digits. The best option to go is use java.util.Locale object and NumberFormat to perform your operations.

Like

LocaleObject=getLocale(country);
NumberFormat=getNUmberformat(LocaleObject);
int value=2345;
int formatedNum=NumberFormat.format(value);
String k="$"+formatedNum;

( I have given vague idea, but it is base, just explore more ...)

With this you can make your number "," seperated for any locale.
 
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Srinivas Kalvala:
Hello,

The requirement is ambiguous.

If you have 2345 number then you can easily convert it into $2345.

as,

int value=2345;
String k="$"+value;

But in your requirement you want to put , to seperate the digits. The best option to go is use java.util.Locale object and NumberFormat to perform your operations.

Like

LocaleObject=getLocale(country);
NumberFormat=getNUmberformat(LocaleObject);
int value=2345;
int formatedNum=NumberFormat.format(value);
String k="$"+formatedNum;

( I have given vague idea, but it is base, just explore more ...)

With this you can make your number "," seperated for any locale.



Shri,

I think Vijaya is looking for opposite.

Vijiya If you are sure that the first character will be $ anyhow.

Then you can use the same method e.g.


Integer.parseInt(money.subString(1));

like this



Hope this helps you.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!