Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java格式负整数_Java_Format_Int - Fatal编程技术网

Java格式负整数

Java格式负整数,java,format,int,Java,Format,Int,我尝试格式化整数,使它们总是有四个正确的数字,也就是说,我想要0作为填充 我尝试了“%04d”,这对正数很有效。对于负片,它会在我需要的地方提供类似于-[854]的内容-[0854] 谢谢。你能发布你尝试过的代码吗?(你可能会对上面的评论感到困惑。你可以尝试解释更多,也就是说,inti=1;System.out.printf(“%04d”,i);而不仅仅是“%04d”)还有,/可能重复的(等等,这是C,但它在这里也起作用,使用“%05d”,请参见第四个标志) if(number>=0){

我尝试格式化整数,使它们总是有四个正确的数字,也就是说,我想要0作为填充

我尝试了
“%04d”
,这对正数很有效。对于负片,它会在我需要的地方提供类似于
-[854]
的内容
-[0854]


谢谢。

你能发布你尝试过的代码吗?(你可能会对上面的评论感到困惑。你可以尝试解释更多,也就是说,
inti=1;System.out.printf(“%04d”,i);
而不仅仅是
“%04d”
)还有,/可能重复的(等等,这是C,但它在这里也起作用,使用
“%05d”
,请参见第四个标志)
if(number>=0){ 
    String.format("%04d",number); 
}else{
     String.format("%05d",number); 
    }