Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a straightforward conversion to std::string, use std::to_string(value). For floating-point values that need controlled formatting, use std::format in C++20 or later. For output into a caller-owned buffer without allocation, use std::to_chars in C++17 or later. The right choice depends on the output you need and the C++ standard-library support in your toolchain.

Choose the conversion method that matches your output

Need Use What it returns or does
A simple numeric conversion to a string std::to_string Returns a std::string; available since C++11. Floating-point output behavior changes in C++26. cppreference: std::to_string
Control over precision or notation std::format Returns a formatted string; available since C++20. cppreference: std::format
Write into a supplied buffer without allocating std::to_chars Writes characters to a buffer and reports the result; available since C++17. cppreference: std::to_chars

Use std::to_string for a simple conversion

Include <string> and pass the number to std::to_string. Integer and floating-point overloads have been available since C++11.

#include <string>

int main() {
    int count = 42;
    std::string text = std::to_string(count); // "42"
}

For floating-point values, be aware of the language-version difference. Before C++26, the function behaves as if it used fixed printf-style formatting. That commonly means six digits after the decimal point: 23.43 can become 23.430000, and a small nonzero value such as 1e-9 can become 0.000000. Before C++26, this formatting also relies on the current C locale. Since C++26, the reference describes the behavior as equivalent to std::format("{}", value). cppreference: std::to_string

Use std::format when the text needs specific formatting

In C++20 or later, include <format> and specify how the value should appear. For example, {:.2f} requests fixed notation with two digits after the decimal point.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#include <format>
#include <string>

int main() {
    double value = 23.43;
    std::string text = std::format("{:.2f}", value); // "23.43"
}

For general notation with six significant digits, use {:.6g}. With no explicit presentation or precision, the default floating-point presentation aims for a shortest round-trippable representation. By contrast, f, e, and g presentations default to precision six if you omit the precision. cppreference: format specification

Use std::to_chars to write into a buffer

Since C++17, std::to_chars in <charconv> can convert a value directly into a character range. It does not allocate, throw, or depend on locale. The result contains an end pointer and an error status; the written characters are not NUL-terminated.

#include <charconv>
#include <string>

int main() {
    int value = 42;
    char buffer[32];
    auto result = std::to_chars(buffer, buffer + sizeof buffer, value);
    if (result.ec == std::errc{}) {
        std::string text(buffer, result.ptr); // "42"
    }
}

On success, the converted range is [buffer, result.ptr). Constructing a string from that range uses the returned end pointer rather than assuming a terminating NUL. If the supplied range is too small, the status is std::errc::value_too_large. cppreference: std::to_chars

Floating-point output with to_chars

Floating-point overloads support fixed, scientific, general, and hexadecimal formats, including overloads with explicit precision. The default representation aims to produce the shortest sequence that the corresponding std::from_chars implementation can parse to recover the value exactly. This round-trip guarantee applies to the matching conversion functions from the same implementation. cppreference: std::to_chars

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Check support in your compiler and standard library

The standard-version labels identify when these facilities were added, but do not guarantee that every installed compiler and standard-library combination supports them. Check your project’s language mode, library implementation, and relevant feature-test macros before relying on std::format or std::to_chars. Support for a particular setup depends on the toolchain version and configuration.

Best Value

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.