2012-07-18 17 views

Respuesta

17

Sólo tiene que utilizar Java'sString.format:

def vals = [ 1, 2, 3, 4 ] 

def strs = vals.collect { 
    String.format("%04d", it) 
} 

strs.each { println it } 

impresiones:

0001 
0002 
0003 
0004 

Otras opciones can be found here

3

Puede utilizar String.format() como se describe en JN1525-Strings

values = [1, 2, 3, 4] 
formatted = values.collect { 
    String.format('%04d', it) 
} 
assert formatted == ['0001', '0002', '0003', '0004'] 
Cuestiones relacionadas