Thread overview
std.string.format
Dec 26, 2006
Egor Starostin
Dec 26, 2006
Don Clugston
Dec 27, 2006
Egor Starostin
Jan 03, 2007
Don Clugston
Dec 26, 2006
Frits van Bommel
Dec 27, 2006
Egor Starostin
December 26, 2006
I have a couple of issues with std.string.format.

1. why
writefln(format("%%1.%df",2));
produces 'Error: std.format'
but
writefln(format(format("%%1.%df",2),1.0));
works fine?

2. on Windows:
format(format("%%1.%df",4),666666/1000000.0) produces "0.6667"
but
format(format("%%1.%df",4),66666/1000000.0) produces "0.0666"

on Linux second line gives correct answer "0.0667"

What can I do with the second problem? Can anyone suggest a workaround?
December 26, 2006
Egor Starostin wrote:
> I have a couple of issues with std.string.format.
> 
> 1. why
> writefln(format("%%1.%df",2));
> produces 'Error: std.format'
> but
> writefln(format(format("%%1.%df",2),1.0));
> works fine?
> 
> 2. on Windows:
> format(format("%%1.%df",4),666666/1000000.0) produces "0.6667"
> but
> format(format("%%1.%df",4),66666/1000000.0) produces "0.0666"
> 
> on Linux second line gives correct answer "0.0667"

This sounds like a C library issue. Are you using GDC, or DMD ?
> 
> What can I do with the second problem? Can anyone suggest a
> workaround?
December 26, 2006
Egor Starostin wrote:
> I have a couple of issues with std.string.format.
> 
> 1. why
> writefln(format("%%1.%df",2));
> produces 'Error: std.format'

Because the format function returns "%1.2f", so writefln expects an extra floating-point argument after that string.
What you may be after is
	writefln("%s", format("%%1.%df",2));

> but
> writefln(format(format("%%1.%df",2),1.0));
> works fine?

Here the outer format call returns "1.00" which is a regular string, so writefln() doesn't have a problem with it.


If you're passing a string to writefln() that contains % signs you want to be sent to the console, either make sure they're properly quoted (i.e. %% instead of %) or pass a format string that expects a string before it (e.g. "%s").
If you don't want them to be sent to the console but want them to be interpreted and substituted, make sure you supply the correct number (and type) of arguments...
December 27, 2006
> > 2. on Windows:
> > format(format("%%1.%df",4),666666/1000000.0) produces "0.6667"
> > but
> > format(format("%%1.%df",4),66666/1000000.0) produces "0.0666"
> >
> > on Linux second line gives correct answer "0.0667"
> This sounds like a C library issue. Are you using GDC, or DMD ?
DMD

In Python for Windows this code works correct. Does this means that Python doesn't use C library in such case?
December 27, 2006
> > 1. why
> > writefln(format("%%1.%df",2));
> > produces 'Error: std.format'
> Because the format function returns "%1.2f", so writefln expects an extra floating-point argument after that string.
Indeed! Thank you for an explanation.
January 03, 2007
Egor Starostin wrote:
>>> 2. on Windows:
>>> format(format("%%1.%df",4),666666/1000000.0) produces "0.6667"
>>> but
>>> format(format("%%1.%df",4),66666/1000000.0) produces "0.0666"
>>>
>>> on Linux second line gives correct answer "0.0667"
>> This sounds like a C library issue. Are you using GDC, or DMD ?
> DMD
> 
> In Python for Windows this code works correct. Does this means that Python doesn't
> use C library in such case?
It uses a different C library (the one which Python was compiled with). The one DMD uses was written by Walter, and supports 80-bit floats, for example.