// Java 中没有直接的 printf 函数,但可以使用 System.out.printf 或者 String.format 来实现类似的功能。
// 示例 1: 使用 System.out.printf 输出格式化字符串
public class Main {
public static void main(String[] args) {
int number = 10;
double pi = 3.14159;
System.out.printf("The number is %d and the value of pi is %.2f", number, pi);
// 输出: The number is 10 and the value of pi is 3.14
}
}
// 示例 2: 使用 String.format 返回格式化后的字符串
public class Main {
public static void main(String[] args) {
int number = 10;
double pi = 3.14159;
String formattedString = String.format("The number is %d and the value of pi is %.2f", number, pi);
System.out.println(formattedString);
// 输出: The number is 10 and the value of pi is 3.14
}
}
在上述代码中:
%d 用于表示整数。%.2f 表示保留两位小数的浮点数。上一篇:string在java中代表什么
下一篇:java定义一个数组
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站