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.
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.
If you divide an int
by another int
you get an int
not a double. Try
x = (double) w[i] / l[i];
System.out.printf("%10.4f", x);
Mind, only BigDecimal has precise precision. Doubles are approximations.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.