aspose file tools*
The moose likes Beginning Java and the fly likes I always get 0 when 1 divided by 3 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I always get 0 when 1 divided by 3" Watch "I always get 0 when 1 divided by 3" New topic
Author

I always get 0 when 1 divided by 3

chaohua wang
Ranch Hand

Joined: Dec 22, 2002
Posts: 62
Hi, Folks,

I ahve this code
long test = 1/3;
System.out.println( test );

but print reult is 0. How to get result 0.33333?

Thank you!

Chwang
Stephen Foy
Ranch Hand

Joined: Oct 17, 2005
Posts: 143
Long isnt suitable for floating point numbers.



Stephen Foy - Microsoft Application Development Consultant
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

1 and 3 are int literals, and int division always truncates. The int result is then widened to type long when it's assigned to the variable "test."

For a floating-point result, at least one of the literals needs to be a float or double. Then the result needs to be assigned to a variable of type float or double.
[ June 01, 2006: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I always get 0 when 1 divided by 3