This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Java in General and the fly likes convert a string having decimal point to double or long Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "convert a string having decimal point to double or long" Watch "convert a string having decimal point to double or long" New topic
Author

convert a string having decimal point to double or long

rakesh krish
Greenhorn

Joined: Jan 09, 2011
Posts: 7
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
Bartender

Joined: Oct 14, 2005
Posts: 18121
    
    8

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
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

Joined: Aug 11, 2007
Posts: 4636
    
    5

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: convert a string having decimal point to double or long
 
Similar Threads
Art and Science of Java - Programming exercise chapter 3
Decimals not converting to correct fractions
Converting a String to the appropriate numeric data type...
Convert double (primitive) value to String
How do i convert string array into long array in java?