-6

Why does Java return a 0 when I divide 10/60? the code I tried is

double pay_per_minute = (10/60);

10 being the pay rate per hour and 60 being the minutes.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Andres Rivera
  • 97
  • 2
  • 7
  • 2
    10 and 60 are integers, the result is 0 even before you convert to a double. – Joachim Isaksson Nov 29 '13 at 09:24
  • Following naming conventions is adviced - substitute `dayPerMinute` for `day_per_minute`. We're not doing – Dropout Nov 29 '13 at 09:27
  • Where is `dayPerMinute` on my question? can't see it. – Andres Rivera Nov 29 '13 at 09:43
  • 1
    As a warning, language like you used in your edit is completely unacceptable here. I've removed this. – Brad Larson Dec 08 '13 at 20:03
  • I also notice that my (CW) answer was unaccepted. Was it a (badly aimed) attempt at taking a revenge against my close vote ? If so, you should dive deeper into SO and accept this was the correct output : you got downvotes because your question was easy to answer with a little search, and I voted to close, even while I answered to help you, because it *was* a duplicate. – Denys Séguret Dec 11 '13 at 07:37
  • @dystroy sorry thought i could delete the question to stop getting so much negative votes. Just accepted your answer again. can't use stackoverflow to ask questions again that's why i was away and forgot to accept your answer again my bad.. – Andres Rivera Dec 11 '13 at 23:28
  • 1
    I've looked at some of your questions. I even upvoted one that wasn't bad. But the general feeling I got is that before asking SO you generally should google a little for tutorials/documentations on the topic you have a problem. Every time I ask SO, I spent hours searching before. To regain your ability to ask questions, you might try to answer some questions. Even if it isn't easy, you'll learn both the ways of SO and your languages (I learned, and still learn, a lot on SO by looking at questions and trying to answer). – Denys Séguret Dec 12 '13 at 09:07
  • Thank you!, will do, That's what I'm doing right at the moment researching everything I can before I go ask. – Andres Rivera Dec 12 '13 at 23:14

5 Answers5

7

Because you're building an integer. When you store it in a double variable, it's already too late : it's 0.

Do

double pay_per_minute = (10.0/60);

If you have variables, cast them :

double pay_per_minute = ((double)pay_per_hour) / 60;
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
0

any primitive digit in java is treated as an integer, so when you do 10/60, it is integer division and due to precision loss it is giving 0

dev2d
  • 4,245
  • 3
  • 31
  • 54
0

Here 10 and 60 takes as int values and then you get int dividing result it is 0 then you get answer as 0. use following way.

double a=10;
double b=60;

double div=a/b;
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

you need to type cast it first because by default numericals are considered as integers

double pay_per_minute = ((double)10/60);
    System.out.println(pay_per_minute);

output 0.16666666666666666

SpringLearner
  • 13,738
  • 20
  • 78
  • 116
0
double pay_per_minute = (10/60);

Here, you are dividing integer 10 by integer 60. So, this is like doing

int temp = 10/60;
double pay_per_minute = double(temp)

your temp will be 0 (since 10/60 is 0 when considered as integer division)

You need to do, double pay_per_minute = (10.0/60);

TheLostMind
  • 35,966
  • 12
  • 68
  • 104