Reputation: 2998
in my code i am performing division on numbers and storing it in a double variable. but the variable is returning with only one precision. i want to increase the precision how could i do that.
double x;
for ( int i =0 ; i<10000 ; i++)
{
x= w[i] / l[i];
System.out.println(x);
}
in the above code w and l are integer arrays;
i am getting output like
3.0
0.0
1.0
0.0
i want to increase the precision upto 4 atleast.
Upvotes: 0
Views: 121
Reputation: 691635
Use
x = ((double) w[i]) / l[i];
As it is, you're dividing two integers using integer division, and assigning the resulting int to a double. Casting one of the operands to a double makes it do a double division instead.
Upvotes: 6
Reputation: 109547
System.out.printf("%10.4f", x);
Mind, only BigDecimal has precise precision. Doubles are approximations.
Upvotes: -1
Reputation: 533442
If you divide an int
by another int
you get an int
not a double. Try
x = (double) w[i] / l[i];
Upvotes: 2