double d = 1000 / 3600; result in to a 0? [duplicate] - java

This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 4 years ago.
I doing a simple calculation from 2 variable and I got in to an issue...
In fact when I try to do " double d = 1000 (which is the first var) / 3600 (which is the 2nd var); it result in a 0. So why ? Any hint about that ?

1000 and 3600 are ints, so when you do 1000 / 3600 you get 0. Then, you are assigning double d to this result of zero. You can instead write 1000.0/3600.0 or if these two numbers are variables, you can cast them to doubles first.

Related

Why do I get 0.0 when I divide these two integers? [duplicate]

This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 2 years ago.
I am trying to show the percentage of day passed using a fixed time. However, when I divide the time passed already by the total amount of time (in seconds) of a day, I get 0.0. I put the current values into the console. Any help is appreciated.
You are performing integer division, and then casting it to a double. You should be doing:
int numOfSecondsSinceMidnight = 61960;
int totalDay = 86400;
double percentDayPassed = 0;
percentDayPassed = (((double)numOfSecondsSinceMidnight / totalDay)*100);
System.out.println(percentDayPassed);
Or better yet, changing numOfSecondsSinceMidnight and totalDay to doubles:
double numOfSecondsSinceMidnight = 61960;
double totalDay = 86400;
double percentDayPassed = 0;
percentDayPassed = ((numOfSecondsSinceMidnight / totalDay)*100);
System.out.println(percentDayPassed);
Both of which print:
71.71296296296296

Java: How do I round a value that is between 0 and 1 to at least 3 decimal places? [duplicate]

This question already has answers here:
Integer division: How do you produce a double?
(11 answers)
Closed 2 years ago.
I'm trying to get a number when I divide 2 numbers together:
int wins = 3070;
int n = 10000;
double probability = wins/n;
System.out.println(probability);
All it prints is: 0.0
But I'm expecting it to print: 0.307
Atleast one of the values (numerator or denominator) should be type casted with double. Integer divided by integer would result integer. If one of them would be double then result would be upcasted to double. Try it! It should work!

Java Operator Precedence issue for a simple equation [duplicate]

This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 4 years ago.
I am trying to implement a simple equation in Java but keep getting the wrong answer apparently due to operator precedence which I am unable to understand.
The equation is:
NewMean = ((N-1) / N) * OldMean + (Xn / N)
in a simple example:
N = 6 ; OldMean = 6 ; Xn = 16
So,
NewMean = 5/6 * 6 + 16/6 = 7.6667 (Correct answer)
but in code implementation on Java i get wrong answer (2.6665):
double NewMean = ((N-1)/N)*oldMean + (Xn/N);
If the N variable is type int, then ((N-1) / N) is computed using integer division and will round 5/6 down to 0. Change N to a floating-point type and you should get the correct answer.

very simple: 1/20 returns 0.0 [duplicate]

This question already has answers here:
Why does the division of two integers return 0.0 in Java? [duplicate]
(6 answers)
Closed 7 years ago.
In Processing I have this code:
int numberOfSteps = 20;
float numberOfStepsCalculated = (1/20);
println(numberOfStepsCalculated);
But my print keeps returning 0.0
I can not figure out why!
Thanks for my dumb question
Integer divison.
float numberOfStepsCalculated = (1.0f / numberOfSteps); // 20
When you divide two int(s) the result is an int (or 0). You then widen the result to a float (or 0.0). Promote one of the values in the calculation to a float and you'll get a float result.

Java divider is giving Zero value [duplicate]

This question already has answers here:
Why double width = 50/110000; the output is 0.000000000000000?
(3 answers)
Closed 9 years ago.
I know this is very stupid question but need to ask,
I have values
int i = 12;
int j = 11;
float k = (i*j)/100;
result is giving 0.0 but here i want more that 2 digits decimal points , How can i achieve it,
I am getting wrong data, it is showing 0 which is wrong
Because all the calculation on the right hand side are of in integer, that is why the result 0.
At least one of the operand should be a floating point number like:
float k= (i * j) / 100.0;
In primary school I learnt integer division. We used to calculate things like 13 divides by 5 is 2 remainder 3. The maths you learnt at school still applies in the computing world. 11 * 12 is 132 and 132 / 100 is 1 and 132 % 100 is 32 (the remainder)
I wouldn't use float as double has half a billion times the accuracy.
double k = (i * j) / 100.0;

Categories