[Java]String.format(); System.out.printf(); System.out.format()

public class FORMAT
{
    public static void main(String[] args) {
        String name = "jerry";
        int age = 50;
        System.out.printf("My name is %s, and I'm %d years old.%n", name, 55);
        System.out.format("%s is %d years old.%n", "George", age + 3);
        //printf与format用法一样

        double cost = 83456.34567;
        String descOfPrice = String.format("\t%s got a 20%% off, so he paid $%,.3f for his new car.%n", "George", cost);
        System.out.print(descOfPrice);

//注:
//flags %,.3f
//%% 来输出%
//我们用%n来换行,而不用\n
//%t 没用,还用\t
    }
}

猜你喜欢

转载自www.cnblogs.com/profesor/p/12971289.html