• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

convert a string having decimal point to double or long

 
rakesh krish
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,


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.


 
Paul Clapham
Sheriff
Pie
Posts: 20980
31
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

How you parse a value and how you display it have nothing in common.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic