1

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);
Ori Lentz
  • 3,668
  • 6
  • 22
  • 28
godfather
  • 19
  • 2

1 Answers1

3

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.

Ori Lentz
  • 3,668
  • 6
  • 22
  • 28