在C ++中将Double转换为String [英] Converting Double to String in C++

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

问题描述

我在尝试将double转换为C ++字符串时遇到一些问题。这是我的代码

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

我遇到的问题是如果双倍传递为'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 ++中将Double转换为String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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