www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - std.string.format

reply Egor Starostin <egorst gmail.com> writes:
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?
Dec 26 2006
next sibling parent reply Don Clugston <dac nospam.com.au> writes:
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?
Dec 26 2006
parent reply Egor Starostin <egorst gmail.com> writes:
 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?
Dec 26 2006
parent Don Clugston <dac nospam.com.au> writes:
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.
Jan 03 2007
prev sibling parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
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...
Dec 26 2006
parent Egor Starostin <egorst gmail.com> writes:
 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.
Dec 26 2006