How do you retain/“re-add” all leading zero(es) in a String of length 9 (that is truncated when being converted from a Long)?

So, question:

How would I “re-add” all leading zero(es) in a String of length 9 (that is truncated when being converted from a Long)?

More details:

In the “front-end” (API), the String value sent in is of length 9, : e.g. “000000000”, “000000001”, “000000010”

Request body:

{
    someObjects: [
        someObject1:    {
            Id: “000000000”;
        ],
        someObject2:    {
            Id:  “000000001”
        },
        someObject3:    {
            Id:  “000000010”
        }
}

The corresponding java class is:

class SomeObject {
    Long id;
}

In the handler method:

public void someRequestHandlerMethod(List<SomeObject> someObjects) {
    for(SomeObject someObject : someObjects) {
        Long Id = someObject.getId();
        LOGGER.info(“Id is: ”+Id);
    }
}

What is happening, is if a number starts with zero(es), when it is converted to a Long, all the zero(es) in front of the remaining number are getting truncated. e.g. “000000000” becomes “0” “000000001” becomes “1” “000000010” becomes “10” and so forth.

So it prints out:

Id is: 0 (instead of Id is: 000000000).
Id is: 1 (instead of Id is: 000000001).
Id is: 10 (instead of Id is: 000000010).

Question: How would I retain the zero(es) or “re-add” them using String manipulation, so that it prints out like:

“000000000” is printed out as “000000000” and not as “0”
“000000001” is printed out as “000000001” and not as “1”
“000000010” is printed out as “000000010” and not as “10”.

Basically, retaining or “re-adding” all leading zero(es) in a number String of lenth 9?

Any help would be greatly appreciated!

thank you so much!

You’re right, converting a String with leading zeros to a Long in Java will remove those zeros. Here’s how you can retain the leading zeros in your scenario:

1. Keep the ID as a String:

  • Instead of converting the ID to a Long, consider keeping it as a String within your SomeObject class. This way, you preserve the leading zeros.

Java

class SomeObject {
    String id; // Keep ID as a String
}

2. String Manipulation:

  • If you absolutely need to convert the ID to a Long for specific operations, use string manipulation to prepend leading zeros back if necessary:

Java

public void someRequestHandlerMethod(List<SomeObject> someObjects) {
    for (SomeObject someObject : someObjects) {
        String idString = someObject.getId();
        Long id = Long.parseLong(idString); // Assuming ID is numeric

        // Pad with leading zeros to maintain 9-digit format
        String formattedId = String.format("%09d", id);

        LOGGER.info("Id is: " + formattedId);
    }
}
  • This code snippet uses String.format("%09d", id) to format the Long value (id ) as a String with a minimum width of 9 (09 ) and right-padding (d ) with zeros.

Choosing the Right Approach:

  • If you only need to display or compare IDs while preserving leading zeros, keeping them as Strings is simpler.
  • If you require mathematical operations on IDs, convert them to Longs but ensure leading zero preservation during manipulation.

Additional Tips:

  • Consider using libraries like Apache Commons Lang or Guava for string manipulation methods like StringUtils.leftPad (if applicable).
  • Make sure the IDs are indeed valid numeric strings before converting them to Longs.

By following these approaches, you can retain the leading zeros in your String IDs and ensure they are handled correctly in your Java application.