使用printf和格式进行格式化 [英] Formatting using printf and format

查看:222
本文介绍了使用printf和格式进行格式化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的程序中:

  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);




$ p
$ b $输出是

  123.456trueA 12365 

请解释如何将double值(即 123.456 )转换为布尔值(即。 true



我问的原因是因为我知道java不允许将数字用于布尔值。例如,Java中不允许使用以下内容:

  if(5){
// do something
}

谢谢

解决方案对于%b,:如果参数arg为null,那么结果是false。如果arg是布尔值或布尔值,则结果是由String.valueOf()返回的字符串。否则,结果是真。

参考


In the following program

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);
    }       
}

The output is

123.456trueA  12365

Can someone please explain how a double value (i.e. 123.456) is converted to a boolean (ie. true)

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
}

Thanks

解决方案

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".

reference

这篇关于使用printf和格式进行格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆