Java的一些格式化操作
大部分的内容可以食用:
https://www.cnblogs.com/Dhouse/p/7776780.html
格式化文本
System.out.println(String.format("长度不满10个字符[左边]补空格:[%10s]", "hello"));
System.out.println(String.format("长度不满10个字符[右边]补空格:[%-10s]", "hello"));
输出:
长度不满10个字符[左边]补空格:[ hello]
长度不满10个字符[右边]补空格:[hello ]
格式化数字
int型
System.out.println(String.format("[%d]", 76));
System.out.println(String.format("[%4d]", 76));
System.out.println(String.format("[%-4d]", 76));
System.out.println(String.format("[%04d]", 76));
输出
[76]
[ 76]
[76 ]
[0076]
float型
float f = 123.4561f;
System.out.println(String.format("[%f]", f));
System.out.println(String.format("[%.2f]", f));
System.out.println(String.format("[%.3f]", f));
输出
[123.456100]
[123.46]
[123.456]
格式化日期时间
待完善
本文详细介绍Java中各种数据类型的格式化操作,包括文本、数字及日期时间的格式化方法,通过实例展示如何使用String.format进行精确控制。
1451

被折叠的 条评论
为什么被折叠?



