Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to convert number to string in C++

📅 2014-Sep-17 ⬩ ✍️ Ashwin Nanjappa ⬩ 📚 Archive

In C++11 and later version, converting integer and floating point values into string can be done using the to_string function provided by string header file.

This example demonstrates this:

#include <iostream>
#include <string>

int main()
{
    int i = 9876;
    float f = 3.14;

    std::string si = std::to_string(i);
    std::string sf = std::to_string(f);

    std::cout << si << " " << sf << std::endl;

    return 0;
}

Tried with: GCC 4.9.1 and Ubuntu 14.04