在C ++中将双精度转换为字符串 [英] Converting Double to String in C++

查看:369
本文介绍了在C ++中将双精度转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi guys
我有一些问题,试图将双转换为C ++字符串。这是我的代码

Hi guys I am having some issues trying to convert a double to C++ string. Here is my code

std::string doubleToString(double val)
{
    std::ostringstream out;
    out << val;
    return out.str();
}

我遇到的问题是如果double传递为'10000000' 。然后返回的字符串值是1e + 007

The problem I have is if a double is being passed in as '10000000'. Then the string value being returned is 1e+007

如何获得字符串值为10000000

How can i get the string value as "10000000"

推荐答案

#include <iomanip>
using namespace std;
// ...
out << fixed << val;
// ...

您也可以考虑使用 setprecision 设置小数位数:

You might also consider using setprecision to set the number of decimal digits:

out << fixed << setprecision(2) << val;

这篇关于在C ++中将双精度转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆