格式化 100 以下的数字,前面加 0

Format numbers under 100 with a 0 in front

我编写了一个程序,可以打印出相邻的 NTS 地图表,但我想将 100 以下的数字格式化为前面有 0。

例如。用户输入 094m 现在的输出是:"The adjacent map sheets are 104P 94N 95D 94L"

我希望它们是:相邻的地图页是 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 是数字前面前导零的个数。