C++11 std::to_string(double) - No trailing zeros

前端 未结 9 1820
孤城傲影
孤城傲影 2020-12-01 06:04

Today I tried out some new functions of the C++11 STL and encountered std::to_string.

Lovely, lovely set of functions. Creating a stringstream object fo

9条回答
  •  日久生厌
    2020-12-01 06:22

    The C++11 Standard explicitely says (21.5/7):

    Returns: Each function returns a string object holding the character representation of the value of its argument that would be generated by calling sprintf(buf, fmt, val) with a format specifier of "%d", "%u", "%ld", "%lu", "%lld", "%llu", "%f", "%f", or "%Lf", respectively, where buf designates an internal character buffer of sufficient size

    for the functions declared in this order:

    string to_string(int val);
    string to_string(unsigned val);
    string to_string(long val);
    string to_string(unsigned long val);
    string to_string(long long val);
    string to_string(unsigned long long val);
    string to_string(float val);
    string to_string(double val);
    string to_string(long double val);
    

    Thus, you cannot control the formatting of the resulting string.

提交回复
热议问题