我用到了很多的编辑框控件,但我给各个控件添加变量时添加的是浮点型变量,不是CString,这样我把浮点数据赋给关联后,显示出来的小数位数太多了,我现在只需要显示两位小数位,要怎么弄呢?如果添加的CString型的变量,我知道这么做。

解决方案 »

  1.   

    CString str;
    str.Format("%.2f",f1);
    SetDlgItemText(IDC_EDIT1,str);
      

  2.   

    CString::Format(_T("%.2f"), ...);
      

  3.   

    有可能是我没有表示清楚吧!
    我知道大家的方法可以实现,但是我已经把所有的都关联好了浮点型变量
    float f_press1;
    float f_airflow1;
    float f_weight1;
    float f_press2;
    float f_airflow2;
    float f_weight2;
    float f_press3;
    float f_airflow3;
    float f_weight3;
    float f_pressair1;
    float f_pressair2;
    DDX_Text(pDX, IDC_EDIT1, f_press1);
    DDX_Text(pDX, IDC_EDIT3, f_airflow1);
    DDX_Text(pDX, IDC_EDIT2, f_weight1);
    DDX_Text(pDX, IDC_EDIT4, f_press2);
    DDX_Text(pDX, IDC_EDIT6, f_airflow2);
    DDX_Text(pDX, IDC_EDIT5, f_weight2);
    DDX_Text(pDX, IDC_EDIT7, f_press3);
    DDX_Text(pDX, IDC_EDIT9, f_airflow3);
    DDX_Text(pDX, IDC_EDIT8, f_weight3);
    DDX_Text(pDX, IDC_EDIT10, f_pressair1);
    DDX_Text(pDX, IDC_EDIT11, f_pressair2);
            //定时器里实时获得计算出的数据
    f_press1=p_Collect->GetData(press1);
    f_airflow1=p_Collect->GetData(airflow1);
    f_weight1=p_Collect->GetData(weight1);
    f_press2=p_Collect->GetData(press2);
    f_airflow2=p_Collect->GetData(airflow2);
    f_weight2=p_Collect->GetData(weight2);
    f_press3=p_Collect->GetData(press3);
    f_airflow3=p_Collect->GetData(airflow3);
    f_weight3=p_Collect->GetData(weight3);
    f_pressair1=p_Collect->GetData(pressair1);
    f_pressair2=p_Collect->GetData(pressair2);
    UpdateData(false);
    这样显示出来的数据小数点后有很多位,我现在只需要小数点后两位,要怎么做,比较简单点的。
      我现在是这样做的
                       f_press1=int(p_Collect->GetData(press1)*100)/100.0;
                     ..........
      

  4.   

    f_press1=int(p_Collect->GetData(press1)*100)/100.0;
    你的控件id已经知道的,用下面这个就可以了,不一定非要通过ddx来设置控件显示的字符下面这样可以很方便的解决你的问题
    CString str;
    str.Format("%.2f",f1);
    SetDlgItemText(IDC_EDIT1,str);
      

  5.   

    例子:
    DDX_Text(pDX, IDC_EDIT7, f_press3);
    在DoDataExchange中,将这句代码换成下面几句:
    if(!pDX->m_bSaveAndValidate)
    {
     CString _str;
     _str.Format("%*.g", f_press3);
     SetDlgItemText(IDC_EDIT7, _str);
    }
    else
    {
      DDX_Text(pDX, IDC_EDIT7, f_press3);
    }