繁体   English   中英

(以Java printf格式的字符串

[英]( in Java printf format string

public class MyClass {
    public static void main(String args[]) {
        double x=2345.6789;
        System.out.printf("My number is %(f",x);
    }
}

不管出于什么原因,尽管我发现有多少个printf引用,单个括号仍在编译和执行,但没有一个将其列为修饰符。 有人知道为什么这样或类似的情况吗?

上面代码的输出:

My number is 2345.678900

您不是在看Java参考。 引用文档中的Java格式字符串语法:

Flag ... Description
...
'('      The result will enclose negative numbers in parentheses

这是一种合法格式,但很少使用,我们可以找到其Javadoc描述:

  // The '(' numeric flag may be used to format negative numbers with // parentheses rather than a minus sign. Group separators are // automatically inserted. formatter.format("Amount gained or lost since last statement: $ %(,.2f", balanceDelta); // -> "Amount gained or lost since last statement: $ (6,217.58)" 

printf语句中“%”和“ f”之间的圆括号是不必要的。

为了表示一个浮点数,语法为%f

小数:%d

单个字符:%c

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM