常规的转换函数,无法去除多余的0。
CString doubleToCString(double data)
{
CString strData;
//判断数据是否小于0.000
if (data < 0.0009) {
strData = _T("0");
}
else
{
strData.Format(_T("%.3f"), data);
}
//去除字符串后面的0
for (int i = strData.GetLength() - 1; i > 0; i--) {
if (strData[i] == '0') {
strData.Delete(i, 1);
continue;
}
else if (strData[i] == '.') {
strData.Delete(i, 1);
break;
}
else {
break;
}
}
return strData;
}
double CStringToDouble(CString data)
{
double dData;
//判断字符串开通是否为0
CString temp = data;
while(1) {
if (temp[0] != '0') {
break;
}
if (temp.GetLength() == 0) {
break;
}
temp.Delete(0, 1);//删除当前字符串第一个为0的字符
}
//去除字符串后面的0
for (int i = temp.GetLength() - 1; i > 0; i--) {
if (temp[i] == '0') {
temp.Delete(i, 1);
continue;
}else if(temp[i] == '.'){
temp.Delete(i, 1);
break;
}
else {
break;
}
}
//判断字符串是否为
if (temp.IsEmpty()) {
dData = 0.000;
return dData;
}
if (temp != temp.SpanIncluding(_T(".0123456789"))) {
dData = 0.000;
return dData;
}
dData = _ttof(data);
return dData;
}