This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 6 years ago.
Why does this program show 0.0?.
Below is my code:
class new1{
public static void main (String[]args){
int discount = 15;
float discount1 = 15/100;
System.out.println(discount1);
}
}
In line float discount1 = 15/100; is evaluated using the integer division.
If you want to get expected result, you should write like this
float discount1 = 15.0/100;
Related
This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 2 years ago.
This must be pretty basic Java question but I can't get why below program outputs 1.0:
public class Testing {
public static void main(String args[]) {
int width = 1920;
int height = 1080;
double ratio = width / height;
System.out.println("Ratio: " + ratio + "");
}
}
I expect no surprises.
You are doing integer division, and assigning the result to a double. If you want floating point division, at least one of the arguments must be a floating point value (as #Eugene suggests).
This question already has answers here:
Division of integers in Java [duplicate]
(7 answers)
Integer division: How do you produce a double?
(11 answers)
Int division: Why is the result of 1/3 == 0?
(19 answers)
Why does integer division code give the wrong answer? [duplicate]
(4 answers)
Closed 4 years ago.
So it is obvious that
int x = 3/4; // x is 0 here
But i do not understand why the following is also the case:
double x = 3/4; // x is also 0 here
With the following i am able to get the result i want:
double x = (double) 3/4; // x is 0.75 here, but why do i have to cast?
This question already has answers here:
Division operation is giving me the wrong result [duplicate]
(3 answers)
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 5 years ago.
Here is my code, Eclipse always return integer value of results, even if h is double. Please help me to fix this.
public static void main(String[] args) {
double h=0.0;
for(int i=1;i<=1000;i++) {
h=h+ 1/i;
}
System.out.println("Harmonic sum "+h);
System.out.println("Harmonic sum "+String.format("%.4f", h));
Result:
Harmonic sum 1.0
Harmonic sum 1,0000
You need to cast the result of your division into double
Try this out:
h=h+ (double)1/i;
I hope this is what you were looking for.
This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 5 years ago.
public class Test {
public static void main(String[] args) {
int test = 1;
System.out.println((double)(Math.pow(test/++test, 2)));
}
0.0 is printed to the screen. Why? Why is the cast not working as expected?
If test is declared this way...
double test = 1;
I get what I expect to print... 0.25.
Why?
I am new to programming and I'm playing around. Reading some of the documentation at this level is next to useless.
You are doing integer division. SO change the line to
System.out.println((Math.pow(test/(double)++test, 2)));
to do double division
This question already has answers here:
Division of integers in Java [duplicate]
(7 answers)
Closed 6 years ago.
Every time I run this test program it display, 0.0 instead of 0.5
Does anyone know how to fix this in Eclipse?
public class Test {
public static void main(String[]args){
double distance;
distance = 1/2;
System.out.println(distance);
}
}
Your division is Integer division. There is no fault of Eclipse. Try following:
distance = 1/2.0;
OR
distance = 1.0/2;
Output:0.5