Friday, August 24, 2012

I used to be a C/C++ programmer.

Certain not-very-much-like-C things in Delphi still trip me up. Like this code which I wrote today, expecting it to zero pad my integer values:
    var
          hour,min:Word;
    begin
             ...
     result := Format('%02d%02d',[hour,min]); 
     // expected '2400', got '24 0'


After a bit of googling, I found that you need this syntax, which seems very unorthodox to me:

   result := Format('%.2d%.2d',[hour,min]); 


As weird as that use of a decimal place in a Format statement involving only two WORD or Integer type variables (hour/min) looks to me, it works just like a %02d format specifier would in C, Java, C#, Python, Perl, Ruby, and Everywhere Else in the Known Universe, other than in Delphi and TurboPascal.






No comments:

Post a Comment