c++浮点数转换字符串

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c++浮点数转换字符串相关的知识,希望对你有一定的参考价值。

#include <sstream>
// 函数功能:根据位数取数据的位数
// @param1 [in] CAL_Tokenizer
// @param2 [in] 浮点数
// @param3 [in] 位数
// return 取好位数的字符串
string WINAPI FloatToStr(float fData1, int nBit )
{
    if (nBit < 0 || nBit > 8)
    {
        nBit = 3;
    }
    ostringstream out;
    out<<fData1;
    CString csTemp = out.str().c_str();
    csTemp.Replace(_T(","),_T(""));
    int nDotPos = csTemp.Find(_T("."));
    if (nDotPos == -1)
    {
        int nPlusCount = nBit;
        csTemp += _T(".");
        while(nPlusCount != 0)
        {
            csTemp += _T("0");
            nPlusCount -= 1;
        }
    }
    else
    {
        int nPlusCount = nBit - (csTemp.GetLength() -1 - nDotPos);
        while(nPlusCount != 0)
        {
            csTemp += _T("0");
            nPlusCount -= 1;
        }
    }
    return csTemp.GetBuffer();
}

以上是关于c++浮点数转换字符串的主要内容,如果未能解决你的问题,请参考以下文章