printf 浮点值的字符串格式

发布于 2024-12-12 17:32:15 字数 483 浏览 1 评论 0原文

我有一个关于使用 printf 的问题。

char str[8];
float val = 2.334563;
sprintf(str, format, val);
printf("val = %s.\n", str);

val = -23.34563;
sprintf(str, format, val);
printf("val = %s.\n", str);

val = -0.02334563;
sprintf(str, format, val);
printf("val = %s.\n", str);

val = 233;
sprintf(str, format, val);
printf("val = %s.\n", str);

预期输出如下:

val = +2.3345
val = -23.345
val = -0.0233
val = +233.00

我需要什么格式字符串?感谢您的关注。

I have a question about using printf.

char str[8];
float val = 2.334563;
sprintf(str, format, val);
printf("val = %s.\n", str);

val = -23.34563;
sprintf(str, format, val);
printf("val = %s.\n", str);

val = -0.02334563;
sprintf(str, format, val);
printf("val = %s.\n", str);

val = 233;
sprintf(str, format, val);
printf("val = %s.\n", str);

The expected output follows:

val = +2.3345
val = -23.345
val = -0.0233
val = +233.00

What format string do I need for that? Thank you for your attention.

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

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

发布评论

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

评论(5

清风无影 2024-12-19 17:32:15

老好人%f怎么了

what happened to the good old %f

余生一个溪 2024-12-19 17:32:15
"%f"

例子

printf("%f\n", floatVal);
"%f"

example

printf("%f\n", floatVal);
快乐很简单 2024-12-19 17:32:15

以下(几乎)可以满足您的要求。请注意,我将 str 数组中的字符数从 7 更改为 8;由于所有输出字符串都包含 7 个字符,因此 sprintf 执行的 NULL 终止将导致缓冲区溢出。

我的结果和你的结果之间的唯一区别是 sprintf 执行的舍入。 AFAIK,解决这个问题的唯一方法是使用 floor 预先截断要打印的数字;例如,要打印 2 位数字而不进行舍入 float f = Floor( 1.8888 * 100 ) / 100;

#include <stdio.h>

int main(void)
{
  char str[8];

  {
    float val = 2.334563f;
    sprintf(str, "%+6.*f", 4, val);
    printf("val = %s.\n", str);
  }

  {
    float val = -23.34563f;
    sprintf(str, "%+6.*f", 3, val);
    printf("val = %s.\n", str);
  }

  {
    float val = -0.02334563f;
    sprintf(str, "%+6.*f", 4, val);
    printf("val = %s.\n", str);
  }

  {
    float val = 233.0f;
    sprintf(str, "%+6.*f", 2, val);
    printf("val = %s.\n", str);
  }

  return 0;
}

输出:

val = +2.3346.
val = -23.346.
val = -0.0233.
val = +233.00.

The following (almost) does what you want. Note that I changed the number of characters in the str array from 7 to 8; since all of your output strings contain 7 characters the NULL termination performed by sprintf will cause buffer overflow otherwise.

The only difference between my results and yours is the rounding performed by sprintf. AFAIK, the only way to get around this is to pre-truncate the number you want to print using floor; for example, to print 2 digits without rounding float f = floor( 1.8888 * 100 ) / 100;

#include <stdio.h>

int main(void)
{
  char str[8];

  {
    float val = 2.334563f;
    sprintf(str, "%+6.*f", 4, val);
    printf("val = %s.\n", str);
  }

  {
    float val = -23.34563f;
    sprintf(str, "%+6.*f", 3, val);
    printf("val = %s.\n", str);
  }

  {
    float val = -0.02334563f;
    sprintf(str, "%+6.*f", 4, val);
    printf("val = %s.\n", str);
  }

  {
    float val = 233.0f;
    sprintf(str, "%+6.*f", 2, val);
    printf("val = %s.\n", str);
  }

  return 0;
}

Output:

val = +2.3346.
val = -23.346.
val = -0.0233.
val = +233.00.
带上头具痛哭 2024-12-19 17:32:15

使用 snprintf() 将字符串截断为 8 个字符,包括 \0

格式字符串:

"%#+.5f"

%   modifier
#   force decimal place
+   show + or -
.5  precision of 5
f   use precision as places after decimal 

代码:

char str[8];
float val = 2.334563;
snprintf(str,8, "%#+.5f", val);
printf("val = %s\n", str);

val = -23.34563;
snprintf(str,8, "%#+.5f", val);
printf("val = %s\n", str);

val = -0.02334563;
snprintf(str,8, "%#+.5f", val);
printf("val = %s\n", str);

val = 233.001;
snprintf(str,8, "%#+.5f", val);
printf("val = %s\n", str);

输出:

val = +2.3345
val = -23.345
val = -0.0233
val = +233.00
val = +0.0003

use snprintf() to truncate string at exactly 8 characters including \0

Format string:

"%#+.5f"

%   modifier
#   force decimal place
+   show + or -
.5  precision of 5
f   use precision as places after decimal 

Code:

char str[8];
float val = 2.334563;
snprintf(str,8, "%#+.5f", val);
printf("val = %s\n", str);

val = -23.34563;
snprintf(str,8, "%#+.5f", val);
printf("val = %s\n", str);

val = -0.02334563;
snprintf(str,8, "%#+.5f", val);
printf("val = %s\n", str);

val = 233.001;
snprintf(str,8, "%#+.5f", val);
printf("val = %s\n", str);

Output:

val = +2.3345
val = -23.345
val = -0.0233
val = +233.00
val = +0.0003
同展鸳鸯锦 2024-12-19 17:32:15

我唯一能想到的是:

const char *format = "val = %+6.*f\n";
int places_past_decimal = ???;
printf(format, places_past_decimal, 2.334563f);

在这种情况下,您需要传递一个附加参数(places_past_decimal),它是数字中剩余的未使用的位数在号码的左侧。

The only thing that I can come up with is as follows:

const char *format = "val = %+6.*f\n";
int places_past_decimal = ???;
printf(format, places_past_decimal, 2.334563f);

In this case, you will need to pass an additional argument (places_past_decimal) which is the number of digits remaining in the number that aren't used by the left side of the number.

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