C++ CString format with %f, double number became very big value
Problem Description:
I am maintaining C++ project but i am not family with C++.
I am facing an issue, we use CString
to convert a double
value to String by
double Dose;
CString Dose2;
if(Dose>0)
{
Dose2.Format("%f",Dose);
}else{
Dose2.Format("0");
}
When I set break point after format %f
line of code, the value before and after totally different.
Can you explain why this problem occurred? Some time the string value is correct, sometime not correct.
Before
After
Solution – 1
I tried to write Dose
value to file before format string, it also had big value.
FILE *fpversion;
fpversion = fopen("doserate.txt", "a");
fprintf(fpversion, "%f", Dose);
fclose(fpversion);
I take a look at source code set Dose
value in another dialog.
if(m_MapDlg.GetSafeHwnd()!=NULL)
{
m_MapDlg.Dose=Result;
}
I tried to make function to set Dose
value as
void CMapDlg::SetDose(double dose) {
this->Dose = dose;
}
And replace
m_MapDlg.Dose=Result;
by
m_MapDlg.SetDose(Result);
The Dose
value got corrected value.
I shared my solution for whom concerned.
Thank @Jonathan Leffler for your suggestion.