• Post Reply Bookmark Topic Watch Topic
  • New Topic

java math problem  RSS feed

 
Mkhaya Tini
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
double x = 12*9/500;
System.out.println(x);

I get zero. Seems like double is not taking any value such as 0.xxxx. I tried float too with the same result. why? How to solve this problem?
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>double x = 12*9/500;

(12*9)/500 is integer division, so the result is 0. Make any number in the numerator or divisor expressions a double, for example:

double x = 12.0*9/50;

or even

double x = 12.0*9.0/500.0;
[ February 08, 2006: Message edited by: Jeff Albertson ]
 
Mkhaya Tini
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. It worked.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!