aspose file tools
The moose likes Java in General and the fly likes Number Format Exception (double to Long) Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Number Format Exception (double to Long)" Watch "Number Format Exception (double to Long)" New topic
Author

Number Format Exception (double to Long)

Chris Cornelius
Ranch Hand

Joined: Aug 02, 2005
Posts: 48
I have an issue with trying convert a double to a Long value. I need to convert the double to Long so that it can be stored in a bean of type Long. Here is the code snippet:



I am getting the following exception:

java.lang.NumberFormatException: For input string: "1230.45"


What am I missing?

Thanks in advance.

Chris
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14672
    
  11

What am I missing?

Long does not include decimals. If that's what you want, you can cast a double to a long and chop off the decimal :


[My Blog]
All roads lead to JavaRanch
Chris Cornelius
Ranch Hand

Joined: Aug 02, 2005
Posts: 48
Thanks Christope,

So it looks like my options are to change the bean type
to either a string or double to keep the decimal intact.

Chris
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 26218
    
  66

Or a BigDecimal object.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Number Format Exception (double to Long)
 
Similar Threads
conversion
casting String to ?
Double rounding
convert a string having decimal point to double or long