View source code
Display the source code in std/format/write.d from which this
page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a
Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.
Requires a signed-in GitHub account. This works well for small changes.
If you'd like to make larger changes you may want to consider using
local clone.
Function std.format.write.formatValue
Formats a value of any type according to a format specifier and writes the result to an output range.
void formatValue(Writer, T, Char)
(
auto ref Writer w,
auto ref T val,
scope const ref FormatSpec!Char f
);
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.
Parameters
Name | Description |
---|---|
w | an output range where the formatted value is written to |
val | the value to write |
f | 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 sformat
for more details.
See Also
formattedWrite
which formats several values at once.
Example
import std .array : appender;
import std .format .spec : singleSpec;
auto writer = appender!string();
auto spec = singleSpec("%08b");
writer .formatValue(42, spec);
writeln(writer .data); // "00101010"
spec = singleSpec("%2s");
writer .formatValue('=', spec);
writeln(writer .data); // "00101010 ="
spec = singleSpec("%+14.6e");
writer .formatValue(42.0, spec);
writeln(writer .data); // "00101010 = +4.200000e+01"
Authors
Walter Bright, Andrei Alexandrescu, and Kenji Hara
License
Copyright © 1999-2022 by the D Language Foundation | Page generated by ddox.