如果 int 的字符串等效长度小于 6,我想在其前面添加零。与 String.format 类似。示例:
int e = 21;
System.out.println(String.format("%06d", e)); //000021
int e = 3564;
System.out.println(String.format("%06d", e)); //003564
我需要的是上述输入210000
和356400
末尾添加零。是否已经实现了一些可以完成这项工作的东西,或者我需要自己编写代码?如果是这样如何处理可变长度?
最佳答案
while (e < 100_000) e *= 10;
不言自明。我认为没有比这更简单的方法了。
编辑:
由于e
可以为0,我将上面的语句包装到一个函数中:
public static String pad(int input) {
if (input == 0) return "000000";
while (input < 100_000) input *= 10;
return "" + input;
}
关于java - 在 int java 前面添加零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56044204/