How to give Precise Format

Asked by At

class Geeks{

static void printInFormat(float a, float b){
    float num = a/b;
    System.out.format("%.7f %.3f\n",num,num); 

please make me understand last line I am new in java not getting this.

1 Answers

0
elyes chine On

The first %.7f takes the first argument (float) rounds it to the 7th digit and inserts it into the result String e.g. : 1.12345678 -> 1.1234568
1.12345671 -> 1.1234567

The same goes for the second one

You can have the same results with : System.out.format("%.7f %.3f\n",num);

The format function has this documentation : https://docs.oracle.com/javase/8/docs/api/java/io/PrintStream.html#format-java.lang.String-java.lang.Object...-