新手请教float 类型的基础问题

baidu_35364666 2016-07-25 05:03:28
float类型的数据 通过计算输出如下:
但是我想让他输出后面不要有多余的0 怎么处理呢,比如下面的就输出 1 0.5 0.05 100
	float i1=1;

CString ssss;
ssss.Format("%f",i1);
AfxMessageBox(ssss);//1.000000

ssss.Format("%f",i1/2);
AfxMessageBox(ssss);//0.500000

ssss.Format("%f",i1/20);
AfxMessageBox(ssss);//0.050000

ssss.Format("%f",i1*100);
AfxMessageBox(ssss);//100.000000
...全文
121 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
schlafenhamster 2016-07-26
  • 打赏
  • 举报
回复
ssss.Format("%.7g", i1/2.1); AfxMessageBox(ssss);//0.4761905 “.后7位
zgl7903 2016-07-25
  • 打赏
  • 举报
回复
Format Specification Syntax: printf and wprintf Functions "%.*g", 7, 相当于 %.7g, 最多7位数, 但是* 可以以变量参数的方式传递格式长度, 比如 ("%.*g", iLen, fVal) If the width specification is an asterisk (*), an int argument from the argument list supplies the value. The width argument must precede the value being formatted in the argument list. A nonexistent or small field width does not cause the truncation of a field; if the result of a conversion is wider than the field width, the field expands to contain the conversion result. g, G The precision specifies the maximum number of significant digits printed. Default Six significant digits are printed, with any trailing zeros truncated.
baidu_35364666 2016-07-25
  • 打赏
  • 举报
回复
引用 1 楼 zgl7903 的回复:

  double i1 = 1.0;
  CString ssss;
  ssss.Format("%.*g", 7, i1);  
谢谢,可以了,但是还是不明白"%.*g", 7 代表的是什么意思
schlafenhamster 2016-07-25
  • 打赏
  • 举报
回复
g double Signed value printed in f or e format, whichever is more compact for the given value and precision. The e format is used only when the exponent of the value is less than –4 or greater than or equal to the precision argument. Trailing zeros are truncated, and the decimal point appears only if one or more digits follow it.
zgl7903 2016-07-25
  • 打赏
  • 举报
回复

  double i1 = 1.0;
  CString ssss;
  ssss.Format("%.*g", 7, i1);  

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧