C++ float转string 不含小数末尾多余的0

使用 ostringstream

#include 
#include 

using namespace std;

int main()
{
    float s = 6.7592;
    cout << to_string(s) << endl;  // 6.759200

    double k = 6.7592;
    cout << to_string(k) << endl;  // 6.759200

    int i = 923342;
    cout << to_string(i) << endl;  // 923342

    ostringstream strStream;
    strStream << s;
    cout << strStream.str() << endl;  // 6.7592

    return 0;
}

 

你可能感兴趣的:(C++)