73

今日、私はC ++ 11 STLのいくつかの新機能を試し、に遭遇しましstd::to_stringた。

素敵な、素敵な関数のセット。たった1回のdoubleからstringへの変換のためにstringstreamオブジェクトを作成することは、私にはいつもやり過ぎのように思えたので、次のようなことができるようになったことをうれしく思います。

std::cout << std::to_string(0.33) << std::endl;

結果?

0.330000

私はそれに完全に満足しているわけではありません。std::to_string末尾のゼロを除外するように指示する方法はありますか?インターネットを検索しましたが、私が見る限り、関数は1つの引数(変換される値)しか取りません。文字列ストリームを使用して「昔」に戻ると、ストリームの幅を設定できますが、変換し直したくありません。

誰かがこの問題に遭遇した/解決策を持っていますか?一部のStackOverflow検索では何も得られませんでした。

(C ++ 11 STLリファレンス:http ://en.cppreference.com/w/cpp/string/basic_string/to_string )

4

9 に答える 9

63

末尾のゼロを削除するだけなら、それは簡単です。

std::string str = std::to_string (f);
str.erase ( str.find_last_not_of('0') + 1, std::string::npos );
于 2012-12-04T18:54:37.407 に答える
41

C ++ 11標準は明示的に(21.5/7)と言っています:

戻り値:各関数は、 "%d"、 "%u"、 "%ldの形式指定子を指定してsprintf(buf、fmt、val)を呼び出すことによって生成される引数の値の文字表現を保持する文字列オブジェクトを返します"、"%lu "、"%lld "、"%llu "、"%f "、"%f "、または"%Lf "。ここで、bufは十分なサイズの内部文字バッファを示します。

この順序で宣言された関数の場合:

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);

したがって、結果の文字列のフォーマットを制御することはできません。

于 2012-12-03T15:55:48.417 に答える
16

std::to_stringフォーマットを制御することはできません。sprintfタイプに適切なフォーマット指定子("%f"この場合)を使用した場合と同じ結果が得られます。

より柔軟性が必要な場合は、より柔軟なフォーマッターが必要になりますstd::stringstream

于 2012-12-03T15:41:14.493 に答える
15

末尾のゼロを除外するには:

std::ostringstream oss;
oss << std::setprecision(8) << std::noshowpoint << double_value;
std::string str = oss.str();

注:#include <sstream>および#include <iomanip>

于 2017-09-26T11:13:51.740 に答える
2

std::to_string(double)は、によって生成されるのと同じ文字シーケンスを返すように標準で定義されていsprintf(buf, "%f", value)ます。これ以上、それ以下、特にフォーマット指定子を微調整する方法はありません。いいえ、あなたにできることは何もありません。

于 2012-12-03T15:51:17.780 に答える
1

With boost::to_string you cannot control the format either but it will output something closer to what you see in the screen. Same with std::lexical_cast<std::string>.

For a function-like operation with format control, use str(boost::format("...format...")% 0.33).

What's the difference between std::to_string, boost::to_string, and boost::lexical_cast<std::string>?

于 2018-09-12T05:48:04.100 に答える
0

to_string動作しないため、問題に対するさまざまな解決策。「魔法の公式」-私のCS2400の先生

std::cout.setf(ios::fixed);
std::cout.setf(ios::showpoint);
std::cout.precision(2);

const double x = 0.33, y = 42.3748;
std::cout << "$" << x << std::endl;
std::cout << "$" << y << std::endl;

出力:

$0.33
$42.37

any following output you do with decimal numbers will be set as so.

you can always change the setf and precision as you see fit.

于 2018-04-18T04:27:47.730 に答える
0

Create a custom convert function, remove the tailing zeros if necessary.

//! 2018.05.14 13:19:20 CST
#include <string>
#include <sstream>
#include <iostream>
using namespace std;

//! Create a custom convert function, remove the tailing zeros if necessary.  
template<typename T>
std::string tostring(const T &n) {
    std::ostringstream oss;
    oss << n;
    string s =  oss.str();
    int dotpos = s.find_first_of('.');
    if(dotpos!=std::string::npos){
        int ipos = s.size()-1;
        while(s[ipos]=='0' && ipos>dotpos){
            --ipos;
        }
        s.erase ( ipos + 1, std::string::npos );
    }
    return s;
}

int main(){
    std::cout<< tostring(1230)<<endl;
    std::cout<< tostring(12.30)<<endl;
}

The input numbers :

1230
12.30

Compile with -std=c++11, then the result:

1230
12.3
于 2018-05-14T05:18:49.917 に答える
0

Write a generic helper function, such as below, which you can repurpose for rounding needs within your C++ project.

inline static const string roundDouble(const double input, const int decimal_places)
{
    ostringstream str;
    str << fixed << setprecision(decimal_places);
    str << input;
    return str.str();
}
于 2021-08-26T12:48:31.833 に答える