Why double width = 50/110000; the output is 0.000000000000000?
Because you're dividing two integers, so it will only take the integer part (integer division).
Dividing integers in a computer program requires special care. Some
programming languages, treat integer division (i.e by giving the integer quotient as the answer). So the answer is an integer.
Examples :
In real life In Java
4/3 = 1.33333 4/3 = 1
25/12 = 2.083333 25/12 = 2
9/2 = 4.5 9/2 = 4
50/110000 = 0.000454545 50/110000 = 0
You can cast one of the number (or both but it's actually useless) to double
to avoid that :
double width = (double)50/110000;
double width = 50d/110000;
double width = 50.0/110000;
Java doing Math not as expected
integer/integer will give you the integer value make any value either denominator or numerator as float you will get the answer
int i = (int) Math.ceil((30 * 50) / 1000.0);
or
int i = (int) Math.ceil((30.0 * 50) / 1000);
or
int i = (int) Math.ceil((30 * 50.0) / 1000);
Java divider is giving Zero value
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;
Incorrect 'double' answer when dividing two 'int' number
Because you are doing integer division, which truncates the result to a integer.
You need to convert either one to double.
double k = i / (double)j;
Program not giving expected output
use
public static void main(String[] args)
{
float num = (float)145/156;
System.out.println(num);
}
As, 145/156
is int/int
, so result is 0, type casted to float i.e 0.0
No answer being displayed. Any ideas?
You are performing integer division with 20/100
, and in Java, dividing int
s yields an int
, the truncated quotient.
Cast one of them as a double
or use double
literals to force floating-point division:
((double) 20 / 100)
or
(20.0 / 100.0)
... and similarly you'll need to change (80/100)
.
Why isn't time calculating correctly?
In the mist of this, I recommend you take a look at Joda Time. It's usually way better to do date math and it takes into consideration often overlooked issues like Timezones, daylight times, etc.
It may be too much, but the jar is small and it will help you with any date/time related operation you have.
Plus it's used by hundreds of apps, so it works.
Related Topics
Is There a Common Java Utility to Break a List into Batches
Java.Io.Console Support in Eclipse Ide
What Operations in Java Are Considered Atomic
What's the Syntax for Mod in Java
Java' Is Not Recognized as an Internal or External Command
How to Handle Windows File Upload Using Selenium Webdriver
How to Access an Iteration-Counter in Java's For-Each Loop
Initializing Multiple Variables to the Same Value in Java
Calculating Distance Between Two Points, Using Latitude Longitude
Upload Files from Java Client to a Http Server
Java Embedded Databases Comparison
How to Use Classes from .Jar Files
Get Domain Name from Given Url
Noclassdeffounderror in Eclipse
How to View My Realm File in the Realm Browser