在C ++中格式化数字

编程入门 行业动态 更新时间:2024-10-27 21:20:39
本文介绍了在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 ++中格式化数字

本文发布于:2023-05-28 12:27:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/320869.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数字

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!