What will be displayed as a result of the following program execution?

public class Main {
    public static void main(String[] args) {
        Formatter formatter = new Formatter(Locale.ROOT);
        formatter.format("%.2E\n", 100.0/3.0);      //1
        formatter.format("%.2f", 100.0/3.0);        //2
        System.out.println(formatter);
    }
}
Explanation
Formatter is a formatting class in Java. 3.33E+01 will be a result of the line 1 execution, since "2" approximates the expression to the 2nd decimal place and E converts it into the engineering representation.
33.33 will be a result of the line 2 execution since a 2nd decimal place approximation is performed here as well.
Without Locale being explicitly specified the result would be dependent on the decimal and integer part separator specified in the system preferences.

formatter.format("%.2E\n", 100.0/3.0); //why 3.33 ???

2024 Apr 20, 5:29:58 PM

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes