printf - Sprintf in Grails -


i got problem in grails, when want create code transfer transaction.

def beforeinsert(){     integer count= transfer.count()+1     integer width= transactionmaster.width      string c = sprintf('%05d',count)      number = transactionmaster.code+"/"+c } 

the code above in variable number, give result : 00007.

now problem is, how make '%05' change according variable width??

for example if width 5, number give result: 00007 if width 2, number give result: 07 if width 10, number give result: 0000000007

hope can , if have solutions, can mail me : medmodest@gmail.com

i open website, :)

this more groovy question, can use groovy gstring.

instead of:

... string c = sprintf('%05d',count) ... 

use this:

... string c = sprintf("%0${width}d",count) ... 

hope helps


Comments