Forum Discussion

bdre's avatar
bdre
Occasional Contributor
7 years ago

zero pad string in groovy script

in my Groovy script I want to format an integer as a zero padded string.  Something like:

 

 

def row = 27
log.info "zero padded = " + Format("%04i", row)

 

This post looked promising but apparently doesn't apply to Groovy:

https://community.smartbear.com/t5/TestComplete-Functional-Web/How-to-use-Format-function-to-format-a-string/m-p/65332/highlight/true#M6053

 

  • Hey, this can be done quite simply:

     

    def row = 27
    def paddedRow = String.format("%010d", row)

    log.info paddedRow

    By "%010d" you specify how many digits do you want your number to have.

     

    Lucian.

  • Lucian's avatar
    Lucian
    Community Hero

    Hey, this can be done quite simply:

     

    def row = 27
    def paddedRow = String.format("%010d", row)

    log.info paddedRow

    By "%010d" you specify how many digits do you want your number to have.

     

    Lucian.

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    That's because this forum is TestComplete and that link is also TestComplete.  Groovy script, IIRC, is part of ReadyAPI/SoapUI Pro.