Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2006-06

Format with zero padding

9 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Micha Nelissen

Hi,

How can I zero-pad using Format ? In C, using printf it's done using an extra 0 digit in for the width: '%04d' with parameter 3 will print '0003'. Wouldn't this be useful for FPC's Format as well, or is there another way ?

Micha

Reply by email

Michael Van Canneyt
to Micha Nelissen: How can I zero-pad using Format ? In C, using printf it's done using an extra 0 digit in…
Micha Nelissen wrote 6 lines
Hi, How can I zero-pad using Format ? In C, using printf it's done using an extra 0 digit in for the width: '%04d' with parameter 3 will print '0003'. Wouldn't this be useful for FPC's Format as well, or is there another way ?

in fpc it's a dot.

Try
%.4d

This is in the docs:
http://www.freepascal.org/docs-html/rtl/sysutils/format.html

Michael.

Reply by email

Micha Nelissen
to Vinzent Hoefler: RTFM. :-) SysUtils.Format ('%.4D', Some_Int); Vinzent.
Vinzent Hoefler wrote 7 lines
On Tuesday 20 June 2006 10:44, Micha Nelissen wrote: > How can I zero-pad using Format ? RTFM. :-) SysUtils.Format ('%.4D', Some_Int);

Sounds like I found a case where 'C' is more intuitive than pascal ;-).

Seriously, isn't "Format" an imitation of C's printf ? At least they are very similar, but on this detail (and maybe some others as well) they are different.

Micha

Reply by email

Vinzent Hoefler
to Micha Nelissen
Micha Nelissen wrote 10 lines
Vinzent Hoefler wrote: > On Tuesday 20 June 2006 10:44, Micha Nelissen wrote: >> How can I zero-pad using Format ? > > RTFM. :-) > > SysUtils.Format ('%.4D', Some_Int); Sounds like I found a case where 'C' is more intuitive than pascal ;-).

As if format strings were intuitive at all. :-)

I'd call "Zero_Pad (Some_Int, 4)" definitely more intuitive.

Seriously, isn't "Format" an imitation of C's printf ?

Apart from the fact, that you can get run time type checking? Yeah, I suppose so.

At least they are very similar, but on this detail (and maybe some others as well) they are different.

Well, let's assume it's due to Delphi-compatibility. Probably Borland decided to do it differently just for the fun of it. ;-)

Vinzent.

Reply by email

Marco van de Voort
to Micha Nelissen: Sounds like I found a case where 'C' is more intuitive than pascal ;-). Seriously, isn't…
> > SysUtils.Format ('%.4D', Some_Int); Sounds like I found a case where 'C' is more intuitive than pascal ;-).

With a C mindset, yes. There a leading 0 can have meaning. In Pascal an extra leading zero never has meaning.

Seriously, isn't "Format" an imitation of C's printf ? At least they are very similar, but on this detail (and maybe some others as well) they are different.

Yeah. Ah well, maybe in C07 that will be fixed.

Reply by email

Micha Nelissen
to Marco van de Voort
Marco van de Voort wrote 5 lines
>> SysUtils.Format ('%.4D', Some_Int); > Sounds like I found a case where 'C' is more intuitive than pascal ;-). With a C mindset, yes. There a leading 0 can have meaning. In Pascal an extra leading zero never has meaning.

It's a formatting string, not a number only. You think a digit *after* the decimal point to be used for integers in front of the whole number is more intuitive ?

Micha

Reply by email

Marco van de Voort
to Micha Nelissen
Show quoted text 10 lines
Marco van de Voort wrote: >>> SysUtils.Format ('%.4D', Some_Int); >> Sounds like I found a case where 'C' is more intuitive than pascal ;-). > > With a C mindset, yes. There a leading 0 can have meaning. In Pascal an > extra leading zero never has meaning. It's a formatting string, not a number only. You think a digit *after* the decimal point to be used for integers in front of the whole number is more intuitive ?

Perfect? no. Better than leading 0? yes.

Leading zeroes that have meaning are an artificial construct. Keep in mind that C users might be aware of deviant use (they are octal numbers as literals), but Pascal users most probably wouldn't think twice.

Reply by email