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

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 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿