printf() 可以生成“-.5”吗?对于值(-0.5)?

发布于 2024-09-26 03:41:08 字数 31 浏览 5 评论 0原文

即可以告诉 printf 忽略小数点前面的零吗?

i.e. can printf be told to ignore zero when it precedes the decimal point?

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

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

发布评论

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

评论(6

抱猫软卧 2024-10-03 03:41:08

对于 ISO C99 中指定的所有内容,这是不可能的。转换说明符的文档说:

f,F: [...] 如果出现小数点字符,则其前面至少出现一位数字。 [...]

e,E: [...] 小数点字符之前有一位数字(如果参数非零,则该数字非零)[...]

g,G: [...] 转换为样式 fe [...]

由于其他转换说明符不会打印十进制浮点数,因此这是根本不可能的。除非您找到引入另一个转换说明符的供应商扩展。但这样你的程序就不再是严格有效的 C 程序了。

我想到的最好的解决方法是:

double number = 0.5;
char buf[80];
char *number = buf;
if (snprintf(buf, sizeof buf, "%f", number) <= 0)
  goto cannot_happen;

while (*number == '0')
  number++;
printf("%s\n", number);

With all the things that are specified in ISO C99, this is not possible. The documentation for the conversion specifiers says:

f,F: [...] If a decimal-point character appears, at least one digit appears before it. [...]

e,E: [...] there is one digit (which is nonzero if the argument is nonzero) before the decimal-point character [...]

g,G: [...] is converted to style f or e [...]

Since the other conversion specifiers would not print a decimal floating-point number, this is impossible at all. Unless you find a vendor extension that introduces another conversion specifier. But then your program would not be strictly valid C anymore.

The best workaround that comes to my mind is:

double number = 0.5;
char buf[80];
char *number = buf;
if (snprintf(buf, sizeof buf, "%f", number) <= 0)
  goto cannot_happen;

while (*number == '0')
  number++;
printf("%s\n", number);
萌辣 2024-10-03 03:41:08

当然可以,但恐怕你对我的回答不会太满意:

printf("-.5", -0.5);

Sure it can, but I'm afraid you won't be too happy with my answer:

printf("-.5", -0.5);
自找没趣 2024-10-03 03:41:08

一句话:不...

In a word: No...

等待圉鍢 2024-10-03 03:41:08

<罢工>是的。使用%g格式说明符代替%f,即printf("%g", -0.5);

如果您对 %g 以指数表示法打印一些数字的方式不满意,另一种方法是使用 snprintf 写入临时字符串缓冲区,然后计算在使用 printf("%.*s", num_nonzero_places, tmp); 写出之前,先检查末尾的零个数,或者您可以删除零并使用 fputs

现在我意识到问题是删除前导零,而不是尾随零。在这种情况下,我知道的最好方法是使用 snprintf 写入临时字符串,然后在写入时跳过第一个字符(如果该字符为“0”)。如果值为负数,您还需要替换“-”号。一种方法是:

snprintf(tmp, sizeof tmp, "%+f", val);
printf("%.*s%s", tmp[0]=='-', tmp, tmp+1+(tmp[1]=='0'));

Yes. Use the %g format specifier instead of %f, i.e. printf("%g", -0.5);

If you're unhappy with the way %g prints some numbers in exponential notation, the alternative is to use snprintf to write to a temporary string buffer, and then count the number of zeros at the end before writing it outwith printf("%.*s", num_nonzero_places, tmp);, or you could just remove the zeros and use fputs.

Now I realize the question is about removing the leading zero, not trailing zeros. In this case, the best way I know is to use snprintf to write to a temporary string, then skip the first character when writing it out if that character is '0'. You'll need to replace the '-' sign too if the value is negative. One way to do this:

snprintf(tmp, sizeof tmp, "%+f", val);
printf("%.*s%s", tmp[0]=='-', tmp, tmp+1+(tmp[1]=='0'));
Smile简单爱 2024-10-03 03:41:08

然后将 double 或 float 转换为 char:

while (*yourChar != '\0') {
    if (yourDoubleBeforeConversion > 1.0 && yourDoubleBeforeConversion < -1.0) {
        continue;
    }
    if(*yourChar == '.') {
        yourChar--;
        *yourChar = '';
    }
    else {
    }
}

说明:

  1. while 循环确定 char 何时结束。
  2. 如果你的双倍大于 1,那么修剪就没有意义了。
  3. 如果不是,请继续检查“.”。
  4. 当找到 1 时,转到前面并删除零。

Convert your double or float to a char then:

while (*yourChar != '\0') {
    if (yourDoubleBeforeConversion > 1.0 && yourDoubleBeforeConversion < -1.0) {
        continue;
    }
    if(*yourChar == '.') {
        yourChar--;
        *yourChar = '';
    }
    else {
    }
}

Explination:

  1. while loop determines when the char ends.
  2. If the double you had is greater than 1 then there is no point in trimming.
  3. If it isn't keep checking for a ".".
  4. When one is found go before and delete the zero.
不及他 2024-10-03 03:41:08

请参阅 printf 规范 和精度部分。你可以这样做 printf("%+0.4f", -0.5);

See the printf spec, and the precision section. You can do printf("%+0.4f", -0.5);

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