我已经编写了一个程序,可以打印出相邻的 NTS map ,但我想对 100 以下的数字进行格式化,并在它们前面加一个 0。
例如。用户输入 094m 现在的输出是:“相邻的 map 是 104P 94N 95D 94L”
我希望它们是:相邻的 map 是 104P 094N 095D 094L
这些是数字的初始变量:
int westBlockNumber = Integer.parseInt(blockNumberAsString);
int eastBlockNumber = Integer.parseInt(blockNumberAsString);
int northBlockNumber =Integer.parseInt(blockNumberAsString);
int southBlockNumber = Integer.parseInt(blockNumberAsString);
最佳答案
您可以使用String.format
引入前导零:
String threeDigitsNumber = String.format("%03d", westBlockNumber);
其中 3
是位数,%0
是数字前面的前导零的数量。
关于java - 格式化 100 以下的数字,前面加 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33678330/