Why do I get 0.0 when I divide these two integers?
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 double
s:
double numOfSecondsSinceMidnight = 61960;
double totalDay = 86400;
double percentDayPassed = 0;
percentDayPassed = ((numOfSecondsSinceMidnight / totalDay)*100);
System.out.println(percentDayPassed);
Both of which print:
71.71296296296296
Why does dividing a float by an integer return 0.0?
It's because you're doing integer division.
Divide by a double or a float, and it will work:
double scale = ( n / 1024.0 ) * 255 ;
Or, if you want it as a float,
float scale = ( n / 1024.0f ) * 255 ;
Division in Java always results in zero (0)?
You're doing integer division.
You need to cast one operand to double
.
Why does java return a 0?
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;
Int division: Why is the result of 1/3 == 0?
The two operands (1 and 3) are integers, therefore integer arithmetic (division here) is used. Declaring the result variable as double just causes an implicit conversion to occur after division.
Integer division of course returns the true result of division rounded towards zero. The result of 0.333...
is thus rounded down to 0 here. (Note that the processor doesn't actually do any rounding, but you can think of it that way still.)
Also, note that if both operands (numbers) are given as floats; 3.0 and 1.0, or even just the first, then floating-point arithmetic is used, giving you 0.333...
.
Why does double z = 1/3 result in 0.0?
The first one is an integer division really. It divides an integer by another integer (the result of which is again an integer) and assigns the result to a double variable.
Only the second one yields a double as result.
integer / integer => result is integer, even though assigned to a double variable
integer or double / double => result is double
Java: Why is this double variable coming out 0?
Make that:
double check = 3.0 / 4;
and it'll work. You got 0
because 3 / 4
is an integer division, whose value is 0
.
Related Topics
Why Does Runtime.Exec(String) Work for Some But Not All Commands
How to Configure Port for a Spring Boot Application
Differences Between Oracle Jdk and Openjdk
Converting Array to List in Java
Is There a Destructor for Java
Moving Decimal Places Over in a Double
Eclipse Java Debugging: Source Not Found
Differencebetween the Hashmap and Map Objects in Java
Java.Text.Parseexception: Unparseable Date
How to Write a Custom JSON Deserializer for Gson
What Is @Modelattribute in Spring MVC
Antlr: Is There a Simple Example
What Is the Easiest/Best/Most Correct Way to Iterate Through the Characters of a String in Java
Convert Date/Time for Given Timezone - Java
How Does Cloneable Work in Java and How to Use It
In Java, How to Convert a Byte Array to a String of Hex Digits While Keeping Leading Zeros