lnhv013710 2019-06-15 23:07 采纳率: 96.7%
浏览 578
已采纳

vs2010 MFC 问一个关于浮点数小数点的处理

我需要处理的数据范围是0-1999.99,这个是在浮点的精度范围内的。
例如如,会遇到如下字符串:
0.00077777->0
0.200000->0.2
2.00000->2
2.55555->2.55
1000.66666->1000.66
1999.00000->1999
->后面是最后想要的字符。
我目前用的下面的办法,我担心这样转换最终会在Section2中出现科学计数法e。不知哪位大神帮我看看能不能行。或者有其他更好的方法

        CString strTemp,Section2
            FLOAT XX=atof(strTemp);  //必须先转浮点数,用来计算。
            strTemp.Format("%.2f",XX); //先转化成字符,截断,保留小数点后两位。
                XX=atof(strTemp);    //再转化成浮点,因为浮点,这里不会小数点后两位。
                Section2.Format("X=%g",XX);//这里好像四舍五入,再去掉零,最后得到想要的结果,
  • 写回答

3条回答

  • threenewbee 2019-06-16 09:25
    关注
    // Q766008.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include "Q766008.h"
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    
    /////////////////////////////////////////////////////////////////////////////
    // The one and only application object
    
    CWinApp theApp;
    
    using namespace std;
    
    void conv1(double d, int* a, int *b)
    {
        // int id = (int)(d * 100 + 0.5);如果要四舍五入,用这个
        int id = (int)(d * 100);
        *a = id / 100;
        *b = id % 100;
    }
    
    void conv(double d, char * buffer)
    {
        int x;
        int y;
        conv1(d, &x, &y);
        if (x == 0 && y == 0)
        {
            sprintf(buffer, "0");
        }
        else if (y == 0)
        {
            sprintf(buffer, "%d", x);
        }
        else if (y < 10)
        {
            sprintf(buffer, "%d.0%d", x, y);
        }
        else
        {
            if (y % 10 != 0)
                sprintf(buffer, "%d.%d", x, y);
            else
                sprintf(buffer, "%d.%d", x, y / 10);
        }
    
    }
    
    int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
    {
        int nRetCode = 0;
    
        // initialize MFC and print and error on failure
        if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
        {
            // TODO: change error code to suit your needs
            cerr << _T("Fatal Error: MFC initialization failed") << endl;
            nRetCode = 1;
        }
        else
        {
            // TODO: code your application's behavior here.
            //CString strHello;
            //strHello.LoadString(IDS_HELLO);
            //cout << (LPCTSTR)strHello << endl;
            double arr[] = { 0.00077777, 0.200000, 2.00000, 2.55555, 1000.66666, 1999.00000 };
            for (int i = 0; i < 6; i++)
            {
                char buffer[100];
                conv(arr[i], buffer);
                CString str = (CString)buffer;
                cout << (LPCTSTR)str << endl;
            }
    
        }
    
        return nRetCode;
    }
    
    
    

    0
    0.2
    2
    2.55
    1000.66
    1999
    Press any key to continue

    本代码的限制:
    转换的值必须是>=0
    转换的值必须小于2100万。

    可以保证在以上条件下满足你的需求

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码