www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - format a string like sprintf?

reply teo <teo.ubuntu yahoo.com> writes:
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
next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
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
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
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:

 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.
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" -Steve
Aug 16 2011
parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Is this what you're after?

import std.string;
void main()
{
    auto str = format("%.4s", 4);
    assert(str == "0004");
}
Aug 16 2011
prev sibling next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
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
parent teo <teo.ubuntu yahoo.com> writes:
On Tue, 16 Aug 2011 09:51:41 -0400, bearophile wrote:

 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
Please ignore my post. It is all my fault. std.string.format is working as expected (at least in my case).
Aug 16 2011
prev sibling parent Vijay Nayar <madric gmail.com> writes:
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