[英]Multiplication of float with int in c
#include <stdio.h>
int main(){
float var = 0.612;
printf("%f\n",var);
printf("%f\n",var*100);
return 0;
}
o/p o / p
0.612000
61.199997
I found that for JavaScript, we have .tofixed()
method. 我发现对于JavaScript,我们有.tofixed()
方法。
How do we get a fix for this in C? 我们如何在C语言中对此进行修复?
You can specify the precision when printing: 您可以在打印时指定精度:
printf("%.3f\n", 100 * var);
Since the exact number you're having probably isn't representable in the float
itself, there is no operation you can do on the number itself to "remove" the decimals, it's all a matter of how you choose to present the data. 由于您所拥有的确切数字可能无法在float
表示出来,因此您无法对数字本身进行任何操作来“去除”小数,这完全取决于您选择如何显示数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.