这是字符串,例如:
"Apple"
我想加零来填充 8 个字符:
"000Apple"
我该怎么做?
原文由 Roy 发布,翻译遵循 CC BY-SA 4.0 许可协议
这是字符串,例如:
"Apple"
我想加零来填充 8 个字符:
"000Apple"
我该怎么做?
原文由 Roy 发布,翻译遵循 CC BY-SA 4.0 许可协议
public class LeadingZerosExample {
public static void main(String[] args) {
int number = 1500;
// String format below will add leading zeros (the %0 syntax)
// to the number above.
// The length of the formatted string will be 7 characters.
String formatted = String.format("%07d", number);
System.out.println("Number with leading zeros: " + formatted);
}
}
原文由 Alex Rashkov 发布,翻译遵循 CC BY-SA 3.0 许可协议
15 回答8.4k 阅读
8 回答6.2k 阅读
1 回答4k 阅读✓ 已解决
3 回答2.2k 阅读✓ 已解决
2 回答3.1k 阅读
2 回答3.8k 阅读
3 回答1.7k 阅读✓ 已解决
如果您必须在没有图书馆帮助的情况下这样做:
(只要您的字符串不超过 8 个字符,就可以工作。)