Semantics of toString

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Fri Nov 6 06:50:14 PST 2009


Leandro Lucarella wrote:
> Yigal Chripun, el  6 de noviembre a las 14:23 me escribiste:
>>>> the c style format string that specifies types is a horrible horrible
>>>> thing and should be removed.
>>> How do you do %.3f in {}-notation?
>> writefln("{0:F3}", value);
>>
>>> Your formatting string should be written as writeln(ansi, " ", iso);
> 
> This is horrible, horrible for internationalization, you just can't assume
> how a language order words.
> 
> Anyway, about the type in the format, I think it's nice, as you just
> proved, tango have it too "{0:F3}" is saying "treat the value as a float
> and format it that way". The deal is, the type should not be used to know
> the size of the parameter in the stack like in C's printf(), it should be
> just a hint to convert the value to another type.
> 
> So, type specification is important. Variables reordering is important
> too, and you even have it in POSIX's printf():
> 
> 	printf("%1$d:%2$.*3$d:%4$.*3$d\n", hour, min, precision, sec);
> 
> (see http://www.opengroup.org/onlinepubs/9699919799/functions/printf.html)
> 
> I like printf()'s format (I don't know if it's just because I'm used to it
> though :).
> 

I think you found a bug in Phobos. I tried this:

import std.stdio;

void main() {
     int hour = 1, min = 2, precision = 2, sec = 3;
     writef("%1$d:%2$.*3$d:%4$.*3$d\n", hour, min, precision, sec);
}

and it prints

1:002:003

But it should really print:

1:02:03

right?


Andrei



More information about the Digitalmars-d mailing list