digitalmars.D.learn - format a string like sprintf?
- teo (3/3) Aug 16 2011 What is the correct way in D to format a string like sprintf? I need to
- Steven Schveighoffer (5/8) Aug 16 2011 I think maybe std.string.format? I realize looking at the docs it's
- Steven Schveighoffer (6/13) Aug 16 2011 bleh, disregard that. I only read that you tried std.format.format, I
- Andrej Mitrovic (7/7) Aug 16 2011 Is this what you're after?
- bearophile (4/7) Aug 16 2011 Why don't you show one or more complete runnable examples that show your...
- teo (3/15) Aug 16 2011 Please ignore my post. It is all my fault. std.string.format is working
- Vijay Nayar (27/30) Aug 16 2011 You can go ahead and use the normal std.format to accomplish this task. ...
What is the correct way in D to format a string like sprintf? I need to pad a number with zeroes. I tried to use std.format.format and std.string.format, but had some strange results.
Aug 16 2011
On Tue, 16 Aug 2011 09:23:26 -0400, teo <teo.ubuntu yahoo.com> wrote:What is the correct way in D to format a string like sprintf? I need to pad a number with zeroes. I tried to use std.format.format and std.string.format, but had some strange results.I think maybe std.string.format? I realize looking at the docs it's *woefully* underdocumented, but I think it works just like writefln or std.format.format but without the need for an output range. -Steve
Aug 16 2011
On Tue, 16 Aug 2011 09:32:47 -0400, Steven Schveighoffer <schveiguy yahoo.com> wrote:On Tue, 16 Aug 2011 09:23:26 -0400, teo <teo.ubuntu yahoo.com> wrote:bleh, disregard that. I only read that you tried std.format.format, I didn't realize you already tried std.string.format. I agree with bearophile, post your "strange results" -SteveWhat is the correct way in D to format a string like sprintf? I need to pad a number with zeroes. I tried to use std.format.format and std.string.format, but had some strange results.I think maybe std.string.format? I realize looking at the docs it's *woefully* underdocumented, but I think it works just like writefln or std.format.format but without the need for an output range.
Aug 16 2011
Is this what you're after? import std.string; void main() { auto str = format("%.4s", 4); assert(str == "0004"); }
Aug 16 2011
teo:What is the correct way in D to format a string like sprintf? I need to pad a number with zeroes. I tried to use std.format.format and std.string.format, but had some strange results.Why don't you show one or more complete runnable examples that show your strange results? Both if there are bugs in Phobos to report in Bugzilla, or if you are using it badly, it's useful to know it. Bye, bearophile
Aug 16 2011
On Tue, 16 Aug 2011 09:51:41 -0400, bearophile wrote:teo:Please ignore my post. It is all my fault. std.string.format is working as expected (at least in my case).What is the correct way in D to format a string like sprintf? I need to pad a number with zeroes. I tried to use std.format.format and std.string.format, but had some strange results.Why don't you show one or more complete runnable examples that show your strange results? Both if there are bugs in Phobos to report in Bugzilla, or if you are using it badly, it's useful to know it. Bye, bearophile
Aug 16 2011
On Tue, 16 Aug 2011 13:23:26 +0000, teo wrote:What is the correct way in D to format a string like sprintf? I need to pad a number with zeroes. I tried to use std.format.format and std.string.format, but had some strange results.You can go ahead and use the normal std.format to accomplish this task. This particular implementation is not very efficient, due to the immutable nature of strings, and the fact that I'm adding one character at a time. If you do this operation a lot, you might want to make a version that works with pointers instead. import std.stdio; import std.format; void sprintf(ref string s, ...) { void putc(dchar c) { s ~= c; } std.format.doFormat(&putc, _arguments, _argptr); } void main() { string testString; // Remember the format string: // % - begins format // 0 - use leading '0's // 6 - we want 6 total chars printed (includes one for decimal) // . - indicate precision (numbers after decimal) // 2 - do not show anything less than 1 cent sprintf(testString, "Your change %s is $%06.2f.", "Bob", 12.3456); // Output: "Your change Bob is $012.35." writeln(testString); }
Aug 16 2011