从 long 转换为 string 时保留前导零

Keep leading zeros when converted from long to string

我正在使用 String.format()long 转换为 String。当传递 >0 的数字时它工作正常,但是当我向数字添加前导零时,我在输出中得到一个不同的数字。我的要求是在传递前导零时向用户显示前导零。请指教

public class CreateContractNumber {
  public static void main(String[] args) {
    long account = 0000123;
    long schedule = 001;
    CreateContractNumber ccn = new CreateContractNumber();
    System.out.println("CONTRACT #: "+ccn.createContNbr(account, schedule));
  }

  private String createContNbr(long account, long schedule) {
    StringBuilder sb = new StringBuilder();
    sb.append(String.format("%07d", account);
    sb.append("-");
    sb.append(String.format("%03d", schedule);
    return sb.toString();
  }
}

实际输出: 合同编号:0000083-001

预期输出: 合同编号:0000123-001

long account = 0000123; 是一个八进制数。 0123 oct 是 83 dec,这使您的输出正确。如果你需要 123 dec 只需写 long account = 123; 因为前导零对存储在 long.

中的值没有影响