C++11 std::to_string(double) - 没有尾随零
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13686482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++11 std::to_string(double) - No trailing zeros
提问by Stijn Frishert
Today I tried out some new functions of the C++11 STL and encountered std::to_string
.
今天尝试了C++11 STL的一些新功能,遇到了std::to_string
.
Lovely, lovely set of functions. Creating a stringstream object for just one double-to-string conversion always seemed overkill to me, so I'm glad we can now do something like this:
可爱,可爱的功能集。只为一次双字符串转换创建一个 stringstream 对象对我来说总是显得有些矫枉过正,所以我很高兴我们现在可以做这样的事情:
std::cout << std::to_string(0.33) << std::endl;
The result?
结果?
0.330000
I'm not entirely content with that. Is there a way to tell std::to_string
to leave out the trailing zeros? I searched the internet, but as far as I can see the function takes only one argument (the value to be converted). Back in 'the old days' with stringstreams, you could set the width of the stream, but I'd rather not convert back.
我对此并不完全满意。有没有办法告诉std::to_string
省略尾随零?我搜索了互联网,但据我所知,该函数只需要一个参数(要转换的值)。回到使用 stringstreams 的“过去”,您可以设置流的宽度,但我宁愿不转换回来。
Anyone encountered this problem before/has a solution? Some StackOverflow searches yielded nothing.
任何人之前遇到过这个问题/有解决方案吗?一些 StackOverflow 搜索没有产生任何结果。
(A C++11 STL reference: http://en.cppreference.com/w/cpp/string/basic_string/to_string)
(C++11 STL 参考:http: //en.cppreference.com/w/cpp/string/basic_string/to_string)
采纳答案by rubenvb
The C++11 Standard explicitely says (21.5/7
):
C++11 标准明确表示 ( 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
返回:每个函数返回一个字符串对象,该对象保存其参数值的字符表示,该对象将通过调用 sprintf(buf, fmt, val) 生成,格式说明符为 "%d", "%u", "%ld "、"%lu"、"%lld"、"%llu"、"%f"、"%f" 或 "%Lf",其中 buf 指定足够大小的内部字符缓冲区
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.
因此,您无法控制结果字符串的格式。
回答by Marshall Clow
If all you want to do is remove trailing zeros, well, that's easy.
如果您只想删除尾随零,那很容易。
std::string str = std::to_string (f);
str.erase ( str.find_last_not_of('0') + 1, std::string::npos );
回答by Mike Seymour
std::to_string
gives you no control over the format; you get the same result as sprintf
with the appropriate format specifier for the type ("%f"
in this case).
std::to_string
让您无法控制格式;您得到的结果sprintf
与类型的适当格式说明符("%f"
在本例中)相同。
If you need more flexibility, then you will need a more flexible formatter - such as std::stringstream
.
如果您需要更大的灵活性,那么您将需要更灵活的格式化程序 - 例如std::stringstream
.
回答by Arne Mertz
std::to_string(double)
is defined by the standard to just return the same sequence of characters that would be generated by sprintf(buf, "%f", value)
. No more, no less, especially no way to tweak the format specifier. So no, there is nothing you can do.
std::to_string(double)
由标准定义为仅返回由sprintf(buf, "%f", value)
. 不多也不少,尤其是无法调整格式说明符。所以不,你无能为力。
回答by alfC
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>
.
随着boost::to_string
你无法控制的格式,要么却会输出更接近于你在屏幕上看到的。与std::lexical_cast<std::string>
.
For a function-like operation with format control, use str(boost::format("...format...")% 0.33)
.
对于具有格式控制的类似函数的操作,请使用str(boost::format("...format...")% 0.33)
.
std::to_string、boost::to_string 和 boost::lexical_cast<std::string> 之间有什么区别?
回答by u3547485
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:
编译-std=c++11
,然后结果:
1230
12.3
回答by Ivan Folgueira Bande
To leave out the trailing zeros:
去掉尾随零:
std::ostringstream oss;
oss << std::setprecision(8) << std::noshowpoint << double;
std::string str = oss.str();
Hope that helps.
希望有帮助。
回答by StackAttack
A varied solution to the problem since to_string
doesn't work.
"The Magic Formula" - my CS2400 teacher
解决问题的不同解决方案,因为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;
Outputs:
输出:
double val
std::wstringstream wss;
wss << val;
cout << wss.str().c_str();
.33
.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.
您可以随时根据需要更改 setf 和 precision。