digitalmars.D - std.string.format
- Egor Starostin (14/14) Dec 26 2006 I have a couple of issues with std.string.format.
- Don Clugston (2/20) Dec 26 2006
- Egor Starostin (3/10) Dec 26 2006 DMD
- Don Clugston (4/15) Jan 03 2007 It uses a different C library (the one which Python was compiled with).
- Frits van Bommel (14/22) Dec 26 2006 Because the format function returns "%1.2f", so writefln expects an
- Egor Starostin (1/6) Dec 26 2006 Indeed! Thank you for an explanation.
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
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
DMD In Python for Windows this code works correct. Does this means that Python doesn't use C library in such case?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 ?
Dec 26 2006
Egor Starostin wrote: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.DMD In Python for Windows this code works correct. Does this means that Python doesn't use C library in such case?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 ?
Jan 03 2007
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
Indeed! Thank you for an explanation.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.
Dec 26 2006