我记得我是用C语言完成的,但是我无法在Java中使用它。
如何在Java中使用printf()方法打印长值?
我尝试使用下面的代码,我真正需要的是以十六进制方式打印long值,如下所示
long 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
}
}
如果要使用16个字符的零填充字符串,且大写的A-F
以0x
为前缀,则应使用:
System.out.printf("0x%016X", l);
您需要将实际参数打印为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
java.util.Formatter
中的完整文档