% sign related in java

In the process of learning java, I found % related content.
Insert image description here
The code used for learning:

public class HelloWorld {
    
    
    public static void main(String []args) {
    
    
       double x = 11.635;
       double y = 2.76;
        System.out.printf("e 的值为 %1f%n", Math.E);
        System.out.printf("exp(%.3f) 为 %.3f%n", x, Math.exp(x));
		 System.out.printf("exp(%.3f) 为 %.3f%n", x, Math.exp(2));
		//%n表示换行
		 System.out.printf("exp(%.3f) 为 %.3f", x, Math.exp(2));
		//double c = 78953368732098.38;
		double c = 11;
		//infinity 无穷大
	    System.out.printf("exp(%.3f) 为 %.3f%n",c, Math.exp(c));
    }
}

output

e 的值为 2.718282
exp(11.635)112983.831
exp(11.635)7.389
exp(11.635)7.389exp(11.000)59874.142

Related explanations of exp function
Insert image description here

————————————————————————————————————————————

%n  换行  相当于 \n

%c  单个字符

%d  十进制整数

%u  无符号十进制数

%f  十进制浮点数

%o  八进制数

%x  十六进制数

%s  字符串

%%  输出百分号

%.4f      四位小数的十进制浮点数

Example

String name = "Huahua";
System.out.printf("Hello, my name is %s", name);//"Hello, my name is Huahua"
int num = 004;
System.out.printf ("%s's student number is %d", name, num);//"Huahua's student number is 004"
As shown above, the printf method receives n parameters, and the first parameter is the string you want to output. , the second parameter is the first format in the first parameter (%s, etc.), the third parameter is the second format in the first parameter (%s, etc.), and so on.

Example

public class Test{
    
    
    public static void main(String args[]){
    
    
        double x = 11.635;
        double y = 2.76;

        System.out.printf("e 的值为 %.4f%n", Math.E);
        System.out.printf("pow(%.3f, %.3f) 为 %.3f%n", x, y, Math.pow(x, y));
    }
}

The output result of the above example is:

e 的值为 2.7183
pow(11.635, 2.760)874.008

Guess you like

Origin blog.csdn.net/budaoweng0609/article/details/129683244