private String toCode(int value, int length) {

        // Convert value to simple integer string
        String valueString = Integer.toString(value);

        // If the resulting string is too long, truncate to the last N digits
        if (valueString.length() > length)
            return valueString.substring(valueString.length() - length);

        // Otherwise, add zeroes until the desired length is reached
        StringBuilder builder = new StringBuilder(length);
        for (int i = valueString.length(); i < length; i++)
            builder.append('0');

        // Return the padded integer string
        builder.append(valueString);
        return builder.toString();

    }
Given an arbitrary integer value, returns a code containing the decimal
representation of that value and exactly the given number of digits. If
the given value has more than the desired number of digits, leading
digits will be truncated to reduce the length. If the given value has
fewer than the desired number of digits, leading zeroes will be added to
increase the length.

@param value
    The value to convert into a decimal code of the given length.

@param length
    The number of digits to include in the code.

@return
    A code containing the decimal value of the given integer, truncated
    or padded such that exactly the given number of digits are present.
extensions/guacamole-auth-totp/src/main/java/org/apache/guacamole/totp/TOTPGenerator.java