Kar*_*uri 4 java printf long-integer
我记得我是用 C 完成的,但我无法在 Java 中使用它。
如何在 Java 中使用 printf() 方法打印长值?
我尝试使用下面的代码,我实际需要的是以十六进制方式打印长值,如下所示
长 l = 32L -----> 0000000000000022
如果我使用 %d 那么它会打印我不想要的整数值......
class TestPrintf()
{
public static void main(String[] args)
{
long l = 100L;
System.out.printf(“%l”+l); // Error unknown format exception
System.out.printf(“%d”+l); // Prints 100
System.out.printf(“%f”+l); // Unknown illegal format conversion float != java.lang.long
}
}
Run Code Online (Sandbox Code Playgroud)
您需要将要打印的实际参数作为方法的下一个参数printf()。连接将不起作用。
System.out.printf("%d%n", 123); // for ints and longs
System.out.printf("%d%n", 12345L); // for ints and longs
System.out.printf("%f%n", (double) 12345L); // for floating point numbers
Run Code Online (Sandbox Code Playgroud)
完整文档在 java.util.Formatter
| 归档时间: |
|
| 查看次数: |
19772 次 |
| 最近记录: |