Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将浮点转换为字符串,精度为&;指定的小数位数? 如何在C++中指定浮点到字符串,同时指定十进制数字的精度和数量?_C++_String_Floating Point - Fatal编程技术网

将浮点转换为字符串,精度为&;指定的小数位数? 如何在C++中指定浮点到字符串,同时指定十进制数字的精度和数量?

将浮点转换为字符串,精度为&;指定的小数位数? 如何在C++中指定浮点到字符串,同时指定十进制数字的精度和数量?,c++,string,floating-point,C++,String,Floating Point,例如:3.14159265359->“3.14”典型的方法是使用stringstream: #include <iomanip> #include <sstream> double pi = 3.14159265359; std::stringstream stream; stream << std::fixed << std::setprecision(2) << pi; std::string s = stream.str();


例如:
3.14159265359->“3.14”

典型的方法是使用
stringstream

#include <iomanip>
#include <sstream>

double pi = 3.14159265359;
std::stringstream stream;
stream << std::fixed << std::setprecision(2) << pi;
std::string s = stream.str();

做这类事情的习惯方法是“打印到字符串”。在C++中,这意味着使用<代码> STD::StrugSuth
std::stringstream ss;
ss << std::fixed << std::setprecision(2) << number;
std::string mystring = ss.str();
std::stringstream-ss;
ss另一种选择是:


。应该添加错误处理,即检查
写入的<0

这里我提供了一个负面示例,您希望在将浮点数转换为字符串时避免使用该示例

float num=99.463;
float tmp1=round(num*1000);
float tmp2=tmp1/1000;
cout << tmp1 << " " << tmp2 << " " << to_string(tmp2) << endl;
注意:num变量可以是接近99.463的任何值,您将得到相同的打印输出。关键是要避免使用方便的c++11“to_string”函数。我花了一段时间才摆脱这个陷阱。最好的方法是stringstream和sprintf方法(C语言)。C++11或更新版本应提供第二个参数,作为要显示的浮点后的位数。现在默认值是6。我这样做是为了其他人不会在这个问题上浪费时间

我写了我的第一个版本,请让我知道如果你发现任何需要修复的错误。您可以使用iomanipulator控制确切的行为。我的功能是显示小数点后的位数

string ftos(float f, int nd) {
   ostringstream ostr;
   int tens = stoi("1" + string(nd, '0'));
   ostr << round(f*tens)/tens;
   return ostr.str();
}
字符串FTO(浮点f,整数nd){
ostringstream ostr;
int tens=stoi(“1”+字符串(nd,'0'));
ostr您可以使用C++20:

其中
2
是一个精度


它不仅比使用iostreams或
sprintf
更短,而且不受区域设置的影响。

这里是一个仅使用std的解决方案。但是,请注意,这只是四舍五入

    float number = 3.14159;
    std::string num_text = std::to_string(number);
    std::string rounded = num_text.substr(0, num_text.find(".")+3);
对于四舍五入的
,它产生:

3.14

该代码将整个浮点转换为字符串,但将所有字符在“.”后剪切2个字符

为什么不创建具有指定精度的临时浮点,然后将其转换为字符串?@Gilad“临时浮点”没有“指定精度”,有时这种方法会出现故障。这个问题基本上类似于问“什么是“%.2f”格式的等价物”?看看这是否只是IO的需要。为什么在这种情况下,
snprintf
会更好?请解释一下……它肯定不会更快,产生更少的代码[我已经编写了一个printf实现,它相当紧凑,只有不到2000行代码,但是宏扩展可以达到2500行左右]@我坚信它会更快。这就是我首先给出这个答案的原因。@Matstpeterson我已经进行了测量(GCC 4.8.1,-O2)这表明snprintf确实花费的时间比snprintf少了17/22倍。我将很快上传代码。@Matstpeterson我的基准可能有缺陷,但stringstream版本在1000000次迭代时所花费的时间是snprintf版本的两倍(Clang 3.7.0,libstdc++,-O2)。请记住,直接写入std::string所拥有的缓冲区是安全的,仅从C++11标准开始。唯一的问题是,这样您实际上并不是在对数字进行舍入。@ChrCury78如我的回答中所述,这只是向下舍入。如果您想进行正常舍入,只需在值后面的数字中添加5。例如
number+0.005
i在我的情况下。
string ftos(float f, int nd) {
   ostringstream ostr;
   int tens = stoi("1" + string(nd, '0'));
   ostr << round(f*tens)/tens;
   return ostr.str();
}
#include <format>

int main() {
  std::string s = std::format("{:.2f}", 3.14159265359); // s == "3.14"
}
#include <fmt/core.h>

int main() {
  std::string s = fmt::format("{:.2f}", 3.14159265359); // s == "3.14"
}
    float number = 3.14159;
    std::string num_text = std::to_string(number);
    std::string rounded = num_text.substr(0, num_text.find(".")+3);
3.14