File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I always get 0 when 1 divided by 3

 
chaohua wang
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 143
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Long isnt suitable for floating point numbers.

 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic