在C++ builder6.0编写的一个小程序中接收的数据为啥不能显示小数点后两位呢??
数据接收部分:
RxBuff=MSComm1->Input;
ByteNum=RxBuff.ArrayHighBound(1);
for(int i=0;i<=ByteNum;i++)buff[i]=RxBuff.GetElement(i);
data1=(1.00*buff[3]*16*16+buff[2])*0.01;
data2=(1.00*buff[5]*16*16+buff[4])*0.01;
data3=(1.00*buff[7]*16*16+buff[6])*0.01;
data4=(1.00*buff[9]*16*16+buff[8])*0.01;
data5=(1.00*buff[11]*16*16+buff[10])*0.01;
data6=(1.00*buff[13]*16*16+buff[12])*0.01;
data7=(1.00*buff[15]*16*16+buff[14])*0.01;
data8=(1.00*buff[17]*16*16+buff[16])*0.01;
data9=(1.00*buff[19]*16*16+buff[18])*0.01;
data10=(1.00*buff[21]*16*16+buff[20])*0.01;
data11=(1.00*buff[23]*16*16+buff[22])*0.01;
data12=(1.00*buff[25]*16*16+buff[24])*0.01;
data13=(1.00*buff[27]*16*16+buff[26])*0.01;
data14=(1.00*buff[29]*16*16+buff[28])*0.01;
data15=(1.00*buff[31]*16*16+buff[30])*0.01;
data16=(1.00*buff[33]*16*16+buff[32])*0.01;
data17=(1.00*buff[35]*16*16+buff[34])*0.01;
data18=(1.00*buff[37]*16*16+buff[36])*0.01;
data19=(1.00*buff[39]*16*16+buff[38])*0.01;
data20=(1.00*buff[41]*16*16+buff[40])*0.01;
temp1=RoundTo(data1, -3);
temp2=RoundTo(data2, -3);
temp3=RoundTo(data3, -3);
temp4=RoundTo(data4, -3);
temp5=RoundTo(data5, -3);
temp6=RoundTo(data6, -3);
temp7=RoundTo(data7, -3);
temp8=RoundTo(data8, -3);
temp9=RoundTo(data9, -3);
temp10=RoundTo(data10, -3);
temp11=RoundTo(data11, -3);
temp12=RoundTo(data12, -3);
temp13=RoundTo(data13, -3);
temp14=RoundTo(data14, -3);
temp15=RoundTo(data15, -3);
temp16=RoundTo(data16, -3);
temp17=RoundTo(data17, -3);
temp18=RoundTo(data18, -3);
temp19=RoundTo(data19, -3);
temp20=RoundTo(data20, -3);
data1=0;
data2=0;
data3=0;
data4=0;
data5=0;
data6=0;
data7=0;
data8=0;
data9=0;
data10=0;
data11=0;
data12=0;
data13=0;
data14=0;
data15=0;
data16=0;
data17=0;
data18=0;
data19=0;
data20=0;
//data21=0;
buff[0]=0;
buff[1]=0;
buff[2]=0;
buff[3]=0;
buff[4]=0;
buff[5]=0;
buff[6]=0;
buff[7]=0;
buff[8]=0;
buff[9]=0;
buff[10]=0;
buff[11]=0;
buff[12]=0;
buff[13]=0;
buff[14]=0;
buff[15]=0;
buff[16]=0;
buff[17]=0;
buff[18]=0;
buff[19]=0;
buff[20]=0;
buff[21]=0;
buff[22]=0;
buff[23]=0;
buff[24]=0;
buff[25]=0;
buff[26]=0;
buff[27]=0;
buff[28]=0;
buff[29]=0;
buff[30]=0;
buff[31]=0;
buff[32]=0;
buff[33]=0;
buff[34]=0;
buff[35]=0;
buff[36]=0;
buff[37]=0;
buff[38]=0;
buff[39]=0;
buff[40]=0;
buff[41]=0;
显示部分:
Memo1->Text=Memo1->Text.sprintf("%0.01f",temp1);
Memo2->Text=Memo2->Text.sprintf("%0.01f",temp2);
Memo3->Text=Memo3->Text.sprintf("%0.01f",temp3);
Memo4->Text=Memo4->Text.sprintf("%0.01f",temp4);
Memo5->Text=Memo5->Text.sprintf("%0.01f",temp5);
Memo6->Text=Memo6->Text.sprintf("%0.01f",temp6);
Memo7->Text=Memo7->Text.sprintf("%0.01f",temp7);
Memo8->Text=Memo8->Text.sprintf("%0.01f",temp8);
Memo10->Text=Memo10->Text.sprintf("%0.01f",temp10);
Memo11->Text=Memo11->Text.sprintf("%0.01f",temp11);
Memo12->Text=Memo12->Text.sprintf("%0.01f",temp12);
Memo13->Text=Memo13->Text.sprintf("%0.01f",temp13);
Memo14->Text=Memo14->Text.sprintf("%0.01f",temp14);
Memo15->Text=Memo15->Text.sprintf("%0.01f",temp15);
Memo16->Text=Memo16->Text.sprintf("%0.01f",temp16);
Memo17->Text=Memo17->Text.sprintf("%0.01f",temp17);
Memo18->Text=Memo18->Text.sprintf("%0.01f",temp18);
Memo19->Text=Memo19->Text.sprintf("%0.01f",temp19);
Memo20->Text=Memo20->Text.sprintf("%0.01f",temp20);
[解决办法]
你写错了,兄弟
Caption = Caption.sprintf("%0.2f",12.23);
[解决办法]
你既然要显示小数点后两位,就直接保留小数点后两位就可以了,保留了3位小数,然后又通过格式化显示2位,会不会让人有疑问?