How do we print division up to 9 digits?
If variable num
holds 2 and variable den
holds 4, then I need to print 0.500000000
double ans=(double)(num)/(double)(den);
System.out.println(ans);
Use String.format:
System.out.println(String.format("%.9f", ans));
Like in C, the %f
tell the format to expect a float/double value, and the .9
instructs it to print 9 digits after the decimal point.