使用printf()输出正确数量的十进制位置?

发布于 2025-01-26 07:12:49 字数 407 浏览 2 评论 0原文

当我输入2时,我希望获得此输出:

value: 2.4

但是当我进行乘法时,我会得到这个:

value: 2.400000

这是我的代码:

#include <stdio.h>

int main()
{
  float num;
  float result;
  
  printf("Number: ");
  scanf("%f", &num);
  
  result = num * 1.2;
  printf("Result: %f", result);
}

我该怎么办?

When I enter 2, I wish to get this output:

value: 2.4

But when I do the multiplication, I am getting this:

value: 2.400000

This is my code:

#include <stdio.h>

int main()
{
  float num;
  float result;
  
  printf("Number: ");
  scanf("%f", &num);
  
  result = num * 1.2;
  printf("Result: %f", result);
}

What can I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一念一轮回 2025-02-02 07:12:49

您可以使用%。nf其中n是小数点之后的数字数。在您的用例中,%。1Fprintf(“结果:%.1f”,结果)


您的代码中还有其他一些问题。您正在使用scanf(),但是您没有检查其返回值。这可能会导致您的代码断开。

scanf()返回其成功解析的参数。如果出于任何原因失败,它不会改变您给出的参数,并且它使输入缓冲区完整。这意味着,每当您再次尝试并从输入缓冲区读取时,它将自动失败,因为

  • 它以前未能 parse it it
  • 它尚未清楚,所以它始终存在。

这将导致无限循环。

为了解决问题,您需要清除输入缓冲区,如果scanf()失败。通过清除缓冲区,我的意思是阅读并丢弃所有内容,直到遇到newline(当您以前按下 Enter 时)。

void getfloat(const char *message, float *f)
{
    while (true) {
        printf("%s: ", message);
        int rc = scanf("%f", f);
        if (rc == 1 || rc == EOF) break; // Break if the user entered a "valid" float number, or EOF is encountered.
        scanf("%*[^\n]"); // Read an discard everything up until a newline is found.
    }
}

您可以在主机中使用它:

int main(void) // Note the void here when a function doesn't take any arguments
{
    float num;
    float result;
    
    getfloat("Number", &num);
    
    result = num * 1.2;
    printf("Result: %.1f", result); // Print only one digit after the decimal point.
}

样本输出:

Number: x
Number: x12.45
Number: 12.75
Result: 15.3

You can specify how many digits you want to print after the decimal point by using %.Nf where N is the number of digits after the decimal point. In your use case, %.1f: printf("Result: %.1f", result).


There are some other issues in your code. You are making use of scanf(), but you are not checking its return value. This may cause your code to break.

scanf() returns the number of arguments it successfully parsed. If, for any reason, it fails, it doesn't alter the arguments you gave it, and it leaves the input buffer intact. This means whenever you try again and read from the input buffer, it will automatically fail since

  • it previously failed to parse it, and
  • it didn't clear it, so it's always there.

This will result in an infinite loop.

To solve the issue, you need to clear the input buffer in case scanf() fails. By clearing the buffer, I mean read and discard everything up until a newline (when you previously pressed Enter) is encountered.

void getfloat(const char *message, float *f)
{
    while (true) {
        printf("%s: ", message);
        int rc = scanf("%f", f);
        if (rc == 1 || rc == EOF) break; // Break if the user entered a "valid" float number, or EOF is encountered.
        scanf("%*[^\n]"); // Read an discard everything up until a newline is found.
    }
}

You can use it in your main like that:

int main(void) // Note the void here when a function doesn't take any arguments
{
    float num;
    float result;
    
    getfloat("Number", &num);
    
    result = num * 1.2;
    printf("Result: %.1f", result); // Print only one digit after the decimal point.
}

Sample output:

Number: x
Number: x12.45
Number: 12.75
Result: 15.3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文