convert a string having decimal point to double or long

I was trying to convert a string having decimal in it to long or double for that matter..ParseInt method was giving me number format exception since it was not able to resolve decimal point in it right? Should i use a delimiter?? Please do suggest..

My method was basically generating a sequence which ended with ".00" and i had to convert it to double or long before i could manipulate on it.Was stuck there. Would really appreciate help on this.

Since "Integer.parseInt()" converts a String to an int, where do you think you might find a method which converts a String to a double? And what might you guess the name of the method to be?

rakesh krish
Greenhorn

Joined: Jan 09, 2011
Posts: 7

posted May 12, 2011 19:46:20

0

Yes parse double would be fine...But the scenario i had was somethin different which went like this...

my method had a system generated sequence. I basically accepted that sequence in sting which meant that the sequence was ending with ".00" .But i had t bring it back to UI in number format and not decimal..( i had to remove that ".00") .

when i do a parse double the string obviously gets converted to a double which ends with ".0" right?? What else can be tried.. I convert that string to double and then typecast it to int from double??? That is extra effort right?? is there any single method that removes these 2 step and makes it 1??

You are confusing some ideas that are really separate. A floating point value of 123.0 can be displayed as 123.0000 on output, it has nothing to do with calling Float.parseFloat().

You may find that the intrinsic lack of precision of floating point numbers causes values to not display as you want, so instead of getting 123.0000 as you expect, you may get 123.0001 but that is also a separate issue.