Range format specifiers in other languages?
H. S. Teoh
hsteoh at quickfur.ath.cx
Mon Oct 12 11:24:47 UTC 2020
On Mon, Oct 12, 2020 at 05:51:21AM +0000, Imperatorn via Digitalmars-d-learn wrote:
> On Monday, 12 October 2020 at 00:59:33 UTC, Adam D. Ruppe wrote:
> > On Monday, 12 October 2020 at 00:46:37 UTC, Imperatorn wrote:
> > > To people trying to learn, why is that % before ( needed in the
> > > format string?
> >
> > The %( ... %) stuff is expanded and repeated for each element inside
> > the given array.
>
> Thanks, it seems there are some pretty powerful formatting options:
>
> https://dlang.org/phobos/std_format.html
Indeed.
%(...%) is one of my favorite because it can be nested, so it's a very
useful quick-n-dirty tool for debugging ranges. With .chunks and .map,
you can format just about any range-based data in a one-liner for
dumping debug info.
Another cool one is the `,` digit-grouper:
import std;
void main() {
writefln("%,2d", 1234567890);
writefln("%,3d", 1234567890);
writefln("%,4d", 1234567890);
writefln("%,3?d", '_', 1234567890);
writefln("%,4?d", '\'', 1234567890);
writefln("%,4?.2f", '\'', 1234567890.123);
}
Output:
12,34,56,78,90
1,234,567,890
12,3456,7890
1_234_567_890
12'3456'7890
12'3456'7890.12
T
--
Век живи - век учись. А дураком помрёшь.
More information about the Digitalmars-d-learn
mailing list