Strings In Depth9 / 15
Up next

Format numbers

#include <print>
#include <format>

int main() {
    double pi = 3.14159;
    int n = 42;
    std::println("{:.2f}", pi);
    // pad to width 5 with leading zeros
    std::println("{:05}", n);
    // hex with the 0x prefix
    std::println("{:#x}", 255);
}
What you're solving

width, padding, and precision