为什么%。2f语法不起作用 [英] Why %.2f like syntax not working
本文介绍了为什么%。2f语法不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用像%.2f这样的语法,这意味着它必须打印最多两个小数位,但它不是那样,而不是它甚至不读取浮点值为什么?
我尝试过:
i want to use syntax like %.2f which means it has to print upto two decimal places but its not taking like that instead of that it is not even reading as float value why?
What I have tried:
printf("ans=%.2f\n",a/b)
推荐答案
可能是因为您的代码使用a的整数值
和b
。尝试将它们声明为float
,而不是int
:
Probably because your code is using integer values fora
andb
. Try declaring them asfloat
, notint
:
#include <stdio.h>
int main()
{
float a = 24;
float b = 7;
printf("Hello, World!\n");
printf("ans = %.2f\n", a / b);
printf("ans = %2.2f\n", a / b);
return 0;
}
确保a / b
是浮动的。
如果a
且b
是整数,/
得到一个整数。
Make surea/b
is a float.
Ifa
andb
are integers,/
result in an integer.
这篇关于为什么%。2f语法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文