为什么%。2​​f语法不起作用 [英] Why %.2f like syntax not working

查看:74
本文介绍了为什么%。2​​f语法不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用像%.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 for a and b. Try declaring them as float, not int:
#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 sure a/b is a float.
If a and b are integers, / result in an integer.


这篇关于为什么%。2​​f语法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆