1

I've written a program that will print out adjacent NTS mapsheets but I want to format the numbers under 100 with a 0 in front of them.

ex. user enters 094m the output now is : "The adjacent map sheets are 104P 94N 95D 94L"

I want them to be: The adjacent map sheets are 104P 094N 095D 094L

These are the initial variables for the numbers:

    int westBlockNumber = Integer.parseInt(blockNumberAsString);
    int eastBlockNumber =  Integer.parseInt(blockNumberAsString);
    int northBlockNumber =Integer.parseInt(blockNumberAsString);
    int southBlockNumber =  Integer.parseInt(blockNumberAsString);

1 Answers1

1

You can use String.format to introduce leading zeros:

String threeDigitsNumber = String.format("%03d", westBlockNumber);

With 3 being the number of digits, and %0 the number of leading zeros in front of a number.

xaviert
  • 5,653
  • 6
  • 29
  • 31