编辑框显示浮点数据的问题 我用到了很多的编辑框控件,但我给各个控件添加变量时添加的是浮点型变量,不是CString,这样我把浮点数据赋给关联后,显示出来的小数位数太多了,我现在只需要显示两位小数位,要怎么弄呢?如果添加的CString型的变量,我知道这么做。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 CString str;str.Format("%.2f",f1);SetDlgItemText(IDC_EDIT1,str); CString::Format(_T("%.2f"), ...); 有可能是我没有表示清楚吧!我知道大家的方法可以实现,但是我已经把所有的都关联好了浮点型变量 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; .......... f_press1=int(p_Collect->GetData(press1)*100)/100.0;你的控件id已经知道的,用下面这个就可以了,不一定非要通过ddx来设置控件显示的字符下面这样可以很方便的解决你的问题CString str;str.Format("%.2f",f1);SetDlgItemText(IDC_EDIT1,str); 例子: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);} LPDISPATCH该怎么用? Vista 下SDL(svn版本)会Crach MFC:SDK开发包如何导入? VC7.0中怎样象6.0中那样观察内存memory 紧急求助最简单的代码: ComboBox关联变量后如何更新获得值 ODBC如何访问多个数据表 怎样得到执行文件的 路径? 两个问题一起问 小问题:怎样删除项目中的一个类? 注册表的表项中 SERIALCOMM中出现好多串口值 对话框 定时器不响应。
str.Format("%.2f",f1);
SetDlgItemText(IDC_EDIT1,str);
我知道大家的方法可以实现,但是我已经把所有的都关联好了浮点型变量
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;
..........
你的控件id已经知道的,用下面这个就可以了,不一定非要通过ddx来设置控件显示的字符下面这样可以很方便的解决你的问题
CString str;
str.Format("%.2f",f1);
SetDlgItemText(IDC_EDIT1,str);
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);
}