postscript

integer to string with leading zeros


Good afternoon,

I'm very new to postscript and looking into coding graphic elements for XMPie applications for post-composition integration.

Something I'm having trouble wrapping my head around is converting an integer to a 3-digit padded string.

IE 1 -> "001", 20 -> "020", 400 -> "400"

Any push in the right direction is very appreciated.


Solution

  • /padthree
    {
      1000 add 7 string cvs 1 3 getinterval
    } def
    
    1 padthree
    

    eg:

    /padthree
    {
      1000 add 7 string cvs 1 3 getinterval
    } def
    1 padthree
    5 padthree
    40 padthree
    stack
    

    outputs

    040
    005
    001
    PS<SSS>