Alex Rivera | Logout

Padding to get specific number of digits in a number

Asked 2012-07-13T07:56:35.657
13
String val = "98"

I need to get the output as 0000098 (7 digits).

I need left padding of zeros to the string or integer value...

The number stored in val is dynamic and may contain any number of digits, but the output should always be 7 digits.

Edit
Report

1 Answer

27

In groovy, you can pad Strings like so:

val.padLeft( 7, '0' )

That will pad the left of the string with zeros until it is 7 chars in length

answered 2012-07-13T08:25:08.020

Your Answer