Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!