Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

In a code, when I try to format a number, the displayed value is 0.0. I don't understand y!
In this code :

C++
CString FmtValues( double const& stdlat)
{
CString fmtStrStdLat, str;

fmtStrStdLat.Format(_T("%.2lf"));
  str.Format(fmtStrStdLat, stdlat);//the stdlat value is str3 0.31850513383466922,
return str;
}

but the return value is 0.0
Posted

Why don't you simply do the following:

CString FmtValues (double stdlat)
{
    CString s;
    s.Format (_T("%.21f"), stdlat);
    return s;
}


A double variable is passed by value, so the const is superfluous. And as Richard pointed out: There should be only one percent sign, not two!

And why 21 decimals? The double type gives you 15 to 17 significant decimal digits. Everything above that is overkill.
 
Share this answer
 
Comments
Sumal.V 19-Jun-12 9:31am    
Oh well, yeah. I always to do try things in complicated way and end up in confusion. B/w it is not 21 decimals, its 2l(2 followed my letter "L")
nv3 19-Jun-12 10:18am    
Oops, sorry about the 21. In my browser a 1 is hard to distinguish from a letter 'l'.

You can simply say _T("%.2f") then. For printf the 'l' makes no difference. For scanf it does!
C#
Missed a "%" in the format!
Changed: fmtStrStdLat.Format(_T("%.2lf"));
To : fmtStrStdLat.Format(_T("%%.2lf")); The code displays the desired values now.
 
Share this answer
 
Comments
Richard MacCutchan 19-Jun-12 8:24am    
This is not correct, you only need a single % character to start a format specifier. What is incorrect is your use of the l specifier in front of the f; see the documentation for further details.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900