Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting double value from the quotient of integers

Tags:

java

int velMperMin = 667;
int distM = 70;
double movT = (distM/velMperMin)*60;

movtT must be equal to 6.30, but it's 0.

like image 453
You Kuper Avatar asked Jan 18 '26 04:01

You Kuper


1 Answers

You need to cast one of the operands of the division to be a double value. Like this:

double movT = ((double)distM/velMperMin)*60;

Your code performs integer division distM/velMperMin and that results in 0. You then multiply that by 60. Still 0. Then you convert to double and now it is 0.0.

Casting one of the operands to double ensures that the division will be a floating point division rather than an integer division and will yield the result you expect.

like image 63
David Heffernan Avatar answered Jan 19 '26 19:01

David Heffernan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!