formatValue

Formats a value of any type according to a format specifier and writes the result to an output range.

More details about how types are formatted, and how the format specifier influences the outcome, can be found in the definition of a format string.

void
formatValue
(
Writer
T
Char
)
(
auto ref Writer w
,
auto ref T val
,
scope const ref FormatSpec!Char f
)

Parameters

w Writer

an output range where the formatted value is written to

val T

the value to write

f FormatSpec!Char

a FormatSpec defining the format specifier

Writer

the type of the output range w

T

the type of value val

Char

the character type used for f

Throws

A FormatException if formatting did not succeed.

Note: In theory this function should be @nogc. But with the current implementation there are some cases where allocations occur. See $(D sformat) for more details.

Examples

import std.array : appender;
import std.format.spec : singleSpec;

auto writer = appender!string();
auto spec = singleSpec("%08b");
writer.formatValue(42, spec);
assert(writer.data == "00101010");

spec = singleSpec("%2s");
writer.formatValue('=', spec);
assert(writer.data == "00101010 =");

spec = singleSpec("%+14.6e");
writer.formatValue(42.0, spec);
assert(writer.data == "00101010 = +4.200000e+01");

See Also

formattedWrite which formats several values at once.

Meta