HIDE NAV

String.format Demo

Main.java


package ceccs;

public class Main {
    public static void main(String[] args) {
        double pi = Math.PI;
        /**
         * Formatting markers include:
                %d - any integer type (short, int, long) prints as integer
                %f - any float point type (float, double)
                %c - character type
                %s - string
                %% - a percent character
         */
        String floatString = String.format("The value of pi is %.1f + %.3fi" , 1.0, pi);
        System.out.println(floatString);

        char someChar = 't';
        String utfCodeString = String.format("the utf code of '%c' is %d", someChar, (int)someChar);
        System.out.println(utfCodeString);

        String gradeRpt = String.format("You got %.1f%%, Good Job!", 100.0);
        System.out.println(gradeRpt);
    }
}