Java的格式化输出
字符串提供的format()方法可用于在Java中打印格式化的输出。
System.out.printf()方法可用于在Java中打印格式化的输出。
下面的示例通过在format()方法中使用特定的语言环境,格式和参数返回格式化的字符串值
示例
import java.util.*;
public class StringFormat {
public static void main(String[] args) {
double e = Math.E;
System.out.format("%f%n", e);
System.out.format(Locale.GERMANY, "%-10.4f%n%n", e);
}
}输出结果
上面的代码示例将产生以下结果。
2.718282 2,7183
以下是格式字符串的另一个示例示例。
示例
public class HelloWorld {
public static void main(String []args) {
String name = "Hello World";
String s1 = String.format("name %s", name);
String s2 = String.format("value %f",32.33434);
String s3 = String.format("value %32.12f",32.33434);
System.out.print(s1);
System.out.print("\n");
System.out.print(s2);
System.out.print("\n");
System.out.print(s3);
System.out.print("\n");
}
}输出结果
上面的代码示例将产生以下结果。
name Hello World value 32.334340 value 32.334340000000
热门推荐
6 保研的祝福语简短
10 年轻20岁祝福语简短
11 朋友结婚祝福语信息简短
12 女孩婚礼贺卡祝福语简短
13 30段点歌简短祝福语
14 虎年春节祝福语图文简短
15 写给后妈祝福语大全简短
16 简短回复生日祝福语
17 校长送毕业祝福语简短
18 毕业立体贺卡祝福语简短