Formatting using printf and format(使用 printf 和 format 进行格式化)
问题描述
在下面的程序中
class ZiggyTest2 {
public static void main(String[] args){
double x = 123.456;
char c = 65;
int i = 65;
System.out.printf("%s",x);
System.out.printf("%b",x);
System.out.printf("%c",c);
System.out.printf("%5.0f",x);
System.out.printf("%d",i);
}
}
输出是
123.456trueA 12365
谁能解释一下双精度值(即123.456
)如何转换为布尔值(即true
)
Can someone please explain how a double value (i.e. 123.456
) is converted to a boolean (ie. true
)
我问的原因是因为我知道 java 不允许将数字用于布尔值.例如,Java 中不允许以下内容
The reason I ask is because I know java does not allow numbers to be used for boolean values. For example, the following is not allowed in Java
if (5) {
//do something
}
谢谢
推荐答案
for "%b" : 如果参数 arg 为 null,则结果为 "false".如果 arg 是布尔值或布尔值,则结果是 String.valueOf() 返回的字符串.否则,结果为真".
for "%b" : If the argument arg is null, then the result is "false". If arg is a boolean or Boolean, then the result is the string returned by String.valueOf(). Otherwise, the result is "true".
参考
这篇关于使用 printf 和 format 进行格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 printf 和 format 进行格式化


- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 获取数字的最后一位 2022-01-01
- 转换 ldap 日期 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01