std::fmtNumber formatting and string padding utilities.
Provides integer-to-string conversions for hexadecimal, binary, and octal bases, plus helpers for padding and repeating strings.
import std.fmt;
fn main() {
println(fmt.to_hex(255)); // "ff"
println(fmt.to_binary(10)); // "1010"
println(fmt.to_octal(8)); // "10"
println(fmt.pad_left("7", 3, "0")); // "007"
}
to_hexConvert an integer to its lowercase hexadecimal string representation.
Negative numbers are prefixed with -.
import std.fmt;
fn main() {
println(fmt.to_hex(255)); // "ff"
println(fmt.to_hex(0)); // "0"
println(fmt.to_hex(-16)); // "-10"
}
to_binaryConvert an integer to its binary string representation.
Negative numbers are prefixed with -.
import std.fmt;
fn main() {
println(fmt.to_binary(10)); // "1010"
println(fmt.to_binary(0)); // "0"
println(fmt.to_binary(-5)); // "-101"
}
to_octalConvert an integer to its octal string representation.
Negative numbers are prefixed with -.
import std.fmt;
fn main() {
println(fmt.to_octal(8)); // "10"
println(fmt.to_octal(0)); // "0"
println(fmt.to_octal(-9)); // "-11"
}
pad_leftPad a string on the left to reach the given width.
If s is already at least width characters, returns s unchanged.
Delegates to string.pad_left — prefer that form when importing std.string
directly; fmt.pad_left is a convenience for code that already imports fmt.
import std.fmt;
fn main() {
println(fmt.pad_left("42", 5, " ")); // " 42"
println(fmt.pad_left("42", 5, "0")); // "00042"
}
pad_rightPad a string on the right to reach the given width.
If s is already at least width characters, returns s unchanged.
Delegates to string.pad_right — prefer that form when importing std.string
directly; fmt.pad_right is a convenience for code that already imports fmt.
import std.fmt;
fn main() {
println(fmt.pad_right("hi", 5, ".")); // "hi..."
}
repeatRepeat a string count times.
import std.fmt;
fn main() {
println(fmt.repeat("ab", 3)); // "ababab"
println(fmt.repeat("*", 5)); // "*****"
}