在 C语言中 双精度和单精度的数,小数点后面分别保留几位啊······· c语言问题 编写一个函数,作用是保留两位小数,对第三位四舍五...

C\u8bed\u8a00\u4e2dprintf\u533a\u5206\u53cc\u7cbe\u5ea6\u548c\u5355\u7cbe\u5ea6\u5417?\u4e3a\u4ec0\u4e48\u7528lf\u8f93\u51fa\u4e0d\u4e86\u53cc\u7cbe\u5ea6\u6d6e\u70b9\u6570?

c\u8bed\u8a00\u4e2d\u7528printf\u8f93\u51fa\u662f\u533a\u5206\u5355\u7cbe\u5ea6\u548c\u53cc\u5ea6\u7684\u3002
\u5982\u679c\u7528\u6237\u5728\u51fd\u6570\u5b9a\u4e49\u65f6\u5b9a\u4e49\u4e3a\u53cc\u7cbe\u5ea6\uff0c\u6700\u540e\u7528printf\u8f93\u51fa\u65f6\u65e2\u53ef\u4ee5\u7528printf("%lf",a);\u4e5f\u53ef\u4ee5\u7528printf("%f",a)\u8f93\u51fa\u3002
\u7528"lf\u201d\u8f93\u51fa\u4e0d\u4e86\u53cc\u7cbe\u5ea6\u53ef\u80fd\u662f\u7528\u6237\u5b9a\u4e49\u65f6\u5b9a\u4e49\u4e3a\u5355\u7cbe\u5ea6\uff0c\u90a3\u4e48\u53ef\u80fd\u4e0d\u80fd\u7528\u53cc\u7cbe\u5ea6\u8f93\u51fa\u3002
\u81f3\u4e8e\u8f93\u51fa\u7b54\u6848\u6709\u65f6\u4e3a\u6b63\u96f6\u6709\u65f6\u4e3a\u8d1f\u96f6\uff0c\u90a3\u4e48\u53ef\u80fd\u662f\u7528\u6237\u6c42\u51fa\u7684\u7b54\u6848\u8d85\u51fa\u7cbe\u5ea6\u8303\u56f4\u4e86\u3002

\u6269\u5c55\u8d44\u6599\uff1a
1\u3001printf()\u51fd\u6570
printf()\u51fd\u6570\u662f\u683c\u5f0f\u5316\u8f93\u51fa\u51fd\u6570, \u4e00\u822c\u7528\u4e8e\u5411\u6807\u51c6\u8f93\u51fa\u8bbe\u5907\u6309\u89c4\u5b9a\u683c\u5f0f\u8f93\u51fa
\u4fe1\u606f\u3002\u5728\u7f16\u5199\u7a0b\u5e8f\u65f6\u7ecf\u5e38\u4f1a\u7528\u5230\u6b64\u51fd\u6570\u3002\u51fd\u6570\u7684\u539f\u578b\u4e3a\uff1a
int printf(const char *format, ...);
\u51fd\u6570\u8fd4\u56de\u503c\u4e3a\u6574\u578b\u3002\u82e5\u6210\u529f\u5219\u8fd4\u56de\u8f93\u51fa\u7684\u5b57\u7b26\u6570\uff0c\u8f93\u51fa\u51fa\u9519\u5219\u8fd4\u56de\u8d1f\u503c\u3002
printf()\u51fd\u6570\u7684\u8c03\u7528\u683c\u5f0f\u4e3a:
printf("", );
2\u3001\u6761\u4ef6\u8bed\u53e5
\u6761\u4ef6\u8bed\u53e5\u53ef\u4ee5\u7ed9\u5b9a\u4e00\u4e2a\u5224\u65ad\u6761\u4ef6\uff0c\u5e76\u5728\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\u5224\u65ad\u8be5\u6761\u4ef6\u662f\u5426\u6210\u7acb\uff0c\u6839\u636e\u5224\u65ad\u7ed3\u679c\u6267\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\uff0c\u4ece\u800c\u6539\u53d8\u4ee3\u7801\u7684\u6267\u884c\u987a\u5e8f\uff0c\u5b9e\u73b0\u66f4\u591a\u7684\u529f\u80fd\u3002
\u53c2\u8003\u8d44\u6599\u6765\u6e90\uff1a\u767e\u5ea6\u767e\u79d1-\u6761\u4ef6\u8bed\u53e5
\u53c2\u8003\u8d44\u6599\u6765\u6e90\uff1a\u767e\u5ea6\u767e\u79d1-printf()

printf\u662f\u8f93\u51fa\u628a
scanf\u624d\u662f\u8f93\u5165
\u6574\u7406\u5982\u4e0b


#include
float fun (double);
int main()
{
double a;
float b;
printf("\u8bf7\u8f93\u5165\u4e00\u4e2a\u6d6e\u70b9\u6570:");
sanf("%f",&a);
b=fun(a);
printf("\u8f6c\u6362\u540e\u7684\u6d6e\u70b9\u6570\u4e3a:%12.2f\n",b);
return 0;
}
float fun (double h)
{
int e=(int)(h*100.0);
e%=10;
if(e>=5)
h+=0.01;
return (h);
}

c语言中double和float的精度并不是按小数点后面多少位来算的
而是按有效数字来算
比如如果是精度为2个有效数字
0.21就是精确的
但是11.1小数点后面的都是不精确的

以下是单双精度的精确位
float 6-7
double 15-16

6位,8位

双精度 小数点后面保留15位
单精度 小数后面保留7位

扩展阅读:3.0是单精度还是双精度 ... c语言双精度浮点数 ... 5.0为啥是双精度 ... 单精度双精度举个例子 ... 双精度 double 浮点型 ... 为什么7.25是双精度 ... 单精度为什么不直接用双精度 ... 单精度和双精度的区别 ... 双精度实数怎么输出 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网