• 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
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

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.
 
What could go wrong in a swell place like "The Evil Eye"? Or with this tiny ad?
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!