Search code examples
javaintegertype-conversionpaddingtalend

Java - Padding an integer


Well, I have a string in my Java code that needs to be converted into an integer with padding of 10. Ex. Consider this is the string... Str = "52112" I need to convert this string into an integer and the result should be like "0000052112". The result should be an integer. Can anyone help me with this, please?


Solution

  • As far as I know you cannot have an integer typed variable with leading zeros. You can pad the number with zeros but then it will become a String.

    Take a look at:

    http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#leftPad(java.lang.String,%20int)

    In order to conform to the signature you have to convert the number to a string first, but that is no great deal.