leading zeros

Developers
OCD Novice
2009-02-06
2012-08-14
  • OCD Novice

    OCD Novice - 2009-02-06

    Is there an easy way to put leading zeros in numeric values? I've noticed that ~Format behaves differently depending on parens around what's being formatted, but I cant figure it out for this.

    --Gary

    /REXX/
    say ' I want sig_Date to be January 31 of the following year in format mm/dd/yy'
    sig_Month = '01'
    sig_Day = '31'

    YY = '08'
    if YY < 69 then / y2k rearing its head still /
    push '20'YY
    else
    push '19'YY
    pull YEAR

    say""
    sig_Year = Year + 1
    sig_Year = SubStr(sig_Year,3,2)

    push sig_Month'/'sig_Day'/'sig_Year
    pull sig_Date
    say sig_Date' This is how I want date to appear'
    say""
    -- why not:

    say ' YY is 'YY
    Sig_Year = (YY + 1)~Format(2)
    push sig_Month'/'sig_Day'/'sig_Year
    pull sig_Date
    say sig_Date" 2008 and before, ~Format doesn't do what I need"

    say ""

    YY = '09'
    say ' YY is 'YY
    Sig_Year = (YY + 1)~Insert('0')

    push sig_Month'/'sig_Day'/'sig_Year
    pull sig_Date

    say sig_Date" ~Insert doesn't work after 2008"

    exit

     
    • OCD Novice

      OCD Novice - 2009-02-06

      Thanks Rick--

      First solution works fine.

      I couldn't get the others to work (even after fixing the quotes). ... does not understand message "FROMUSADATE"

      --Gary

       
    • Rick McGuire

      Rick McGuire - 2009-02-06

      Try (yy + 1)~right(2, '0')

       
    • Rick McGuire

      Rick McGuire - 2009-02-06

      Of course, an even better means would be to use a DateTime object, which takes care of all of this for you., and even allow you do choose your format.

      date = .DateTime~new(date('year'), 1, 31)
      say date~USAdate

      or, you can also have the DataTime object do the math for you.

      date = .DateTime~fromUSADate("01/31/08').addYears(1)
      say date~USAdate

       

Log in to post a comment.