Java 这些符号字符串是什么意思:%02d %01d?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3377688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:43:09  来源:igfitidea点击:

what do these symbolic strings mean: %02d %01d?

javaprintf

提问by dRef90

I'm looking at a code line similar to:

我正在查看类似于以下内容的代码行:

sprintf(buffer,"%02d:%02d:%02d",hour,minute,second);

I think the symbolic strings refer to the number of numeric characters displayed per hour, minute etc - or something like that, I am not entirely certain.

我认为符号字符串是指每小时、每分钟等显示的数字字符数 - 或类似的东西,我并不完全确定。

Normally I can figure this sort of thing out but I have been unable to find any useful reference searching "%02d %01d" on google. Anyone able to shed some light on this for me?

通常我可以弄清楚这种事情,但我一直无法在谷歌上找到任何有用的参考搜索“%02d %01d”。任何人都能够为我阐明这一点?

采纳答案by AlexanderMP

http://www.cplusplus.com/reference/clibrary/cstdio/printf/

http://www.cplusplus.com/reference/clibrary/cstdio/printf/

the same rules should apply to Java.

同样的规则也适用于 Java。

in your case it means output of integer values in 2 or more digits, the first being zero if number less than or equal to 9

在您的情况下,这意味着输出 2 位或更多位的整数值,如果数字小于或等于 9,则第一个为零

回答by Daniel Bleisteiner

The answer from Alexander refers to complete docs...

亚历山大的回答是指完整的文档......

Your simple example from the question simply prints out these values with 2 digits - appending leading 0 if necessary.

问题中的简单示例只是用 2 位数字打印出这些值 - 如有必要,附加前导 0。

回答by Anax

Instead of Googling for %02dyou should have been searching for sprintf()function.

而不是谷歌搜索%02d你应该一直在寻找sprintf()功能。

%02dmeans "format the integer with 2 digits, left padding it with zeroes", so:

%02d意思是“用 2 位数字格式化整数,用零填充它”,所以:

Format  Data   Result
%02d    1      01
%02d    11     11

回答by Marco Mariani

http://en.wikipedia.org/wiki/Printf#printf_format_placeholders

http://en.wikipedia.org/wiki/Printf#printf_format_placeholders

The article is about the classof printf functions, in several languages, from the 50s to this day.

这篇文章是关于50 年代至今的多种语言的 printf 函数

回答by polygenelubricants

They are formatting String. The Java specific syntax is given in java.util.Formatter.

他们正在格式化String。Java 特定的语法在java.util.Formatter.

The general syntax is as follows:

一般语法如下:

   %[argument_index$][flags][width][.precision]conversion

%02dperforms decimal integer conversion d, formatted with zero padding (0flag), with width 2. Thus, an intargument whose value is say 7, will be formatted into "07"as a String.

%02d执行十进制整数转换d,用零填充(0标志)格式化,宽度为2。因此,int值为 say的参数7将被格式化"07"为 a String

You may also see this formatting string in e.g. String.format.

您也可以在例如String.format.



Commonly used formats

常用格式

These are just some commonly used formats and doesn't cover the syntax exhaustively.

这些只是一些常用的格式,并没有详尽地涵盖语法。

Zero padding for numbers

数字的零填充

System.out.printf("Agent %03d to the rescue!", 7);
// Agent 007 to the rescue!

Width for justification

对齐宽度

You can use the -flag for left justification; otherwise it'll be right justification.

您可以使用-标志进行左对齐;否则这将是正确的理由。

for (Map.Entry<Object,Object> prop : System.getProperties().entrySet()) {
    System.out.printf("%-30s : %50s%n", prop.getKey(), prop.getValue());
}

This prints something like:

这会打印如下内容:

java.version                   :                                 1.6.0_07
java.vm.name                   :               Java HotSpot(TM) Client VM
java.vm.vendor                 :                    Sun Microsystems Inc.
java.vm.specification.name     :       Java Virtual Machine Specification
java.runtime.name              :          Java(TM) SE Runtime Environment
java.vendor.url                :                     http://java.sun.com/

For more powerful message formatting, you can use java.text.MessageFormat. %nis the newline conversion (see below).

要获得更强大的消息格式,您可以使用java.text.MessageFormat. %n是换行符转换(见下文)。

Hexadecimal conversion

十六进制转换

System.out.println(Integer.toHexString(255));
// ff

System.out.printf("%d is %<08X", 255);
// 255 is 000000FF

Note that this also uses the <relative indexing (see below).

请注意,这也使用<相对索引(见下文)。

Floating point formatting

浮点格式

System.out.printf("%+,010.2f%n", 1234.567);
System.out.printf("%+,010.2f%n", -66.6666);
// +01,234.57
// -000066.67

For more powerful floating point formatting, use DecimalFormatinstead.

要获得更强大的浮点格式,请DecimalFormat改用。

%nfor platform-specific line separator

%n用于平台特定的行分隔符

System.out.printf("%s,%n%s%n", "Hello", "World");
// Hello,
// World

%%for an actual %-sign

%%对于实际%符号

System.out.printf("It's %s%% guaranteed!", 99.99);
// It's 99.99% guaranteed!

Note that the doubleliteral 99.99is autoboxed to Double, on which a string conversion using toString()is defined.

请注意,double文字99.99被自动装箱为Double,在其toString()上定义了字符串转换 using 。

n$for explicit argument indexing

n$用于显式参数索引

System.out.printf("%1$s! %1$s %2$s! %1$s %2$s %3$s!",
    "Du", "hast", "mich"
);
// Du! Du hast! Du hast mich!

<for relative indexing

<相对索引

System.out.format("%s?! %<S?!?!?", "Who's your daddy");
// Who's your daddy?! WHO'S YOUR DADDY?!?!?


Related questions

相关问题

回答by Rajnish

%is a special character you put in format strings, for example in C language printf and scanf (and family), that basically says "this is a placeholder for something else, not to be printed/read literally."

%是您放入格式字符串中的特殊字符,例如在 C 语言 printf 和 scanf(以及系列)中,它基本上表示“这是其他内容的占位符,不能按字面意思打印/阅读”。

For example, a (%02d)in a format string for printf is a placeholder for an integer variable that will be printed in decimal (%02d)and padded to at least two digits, with zeros if necessary.

The actual integer is supplied by you in an an argument to the function, e.g. printf("%02d",5);would print 05on screen, (%01d)also works similarly

例如,(%02d)printf 格式字符串中的 a 是整数变量的占位符,该变量将以十进制打印 (%02d)并填充为至少两位数字,如有必要,可以使用零。

实际整数由您在函数的参数中提供,例如printf("%02d",5);05在屏幕上打印,(%01d)也类似

Same hold for Java and other languages too :)

Java 和其他语言也同样适用 :)