本文介绍了在C ++中格式化数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好, 在代码中,当我尝试格式化数字时,显示的值为0.0.我听不懂! 在此代码中:
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 :
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; }但是返回值为0.0
but the return value is 0.0
推荐答案为什么不简单地执行以下操作: Why don''t you simply do the following: CString FmtValues (double stdlat) { CString s; s.Format (_T("%.21f"), stdlat); return s; }
一个双精度变量按值传递,因此const是多余的.正如理查德指出的那样:应该只有一个百分号,而不是两个! 为什么是21位小数?双精度型为您提供15到17个有效的十进制数字.上面的所有内容都不过分.
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.
Missed a "%" in the format! Changed: fmtStrStdLat.Format(_T("%.2lf")); To : fmtStrStdLat.Format(_T("%%.2lf")); The code displays the desired values now.
更多推荐
在C ++中格式化数字
发布评论