我在哪里添加 space 在此 C printf 片段中?

Where am I adding the space in this C printf snippet?

void display(const char *label, double used,
             int const warning, int const critical, int const decimals)
{
  if (critical != 0 && used > critical) {
    printf("<span color='%s'>%s</span><span color='%s'>", COLOR_RED, label, COLOR_RED);
  } else if (warning != 0 && used > warning) {
    printf("<span color='%s'>%s</span><span color='%s'>", COLOR_ORANGE, label, COLOR_ORANGE);
  } else {
    printf("<span color='%s'>%s</span><span color='%s'>", COLOR_GREEN, label, COLOR_GREEN);
  }

  printf("%*.*lf</span>\n", decimals + 3 + 1, decimals, used);
}

长时间访问此代码(和 C),上面的代码片段导致...

<span color='#00FF00'></span><span color='#00FF00'> 2.26</span>

...这是预期的,除了 2.26 前面的 <SPACE>。我在哪里添加它?我该如何摆脱它??!!

这没关系,因为它与您的格式相关联。您正在使用 %*.*lf,但根据您当前的结果,您的 decimals 变量的值为 2(因为结果在小数点后有 2 位数字)。

那么,您的格式相当于 %6.2lf,输入数据为 2.26,结果为 2 个空格,最终结果为 2.26

如果要完全删除前面的空格,请使用 %.*lf 并仅传递 decimals 作为参数,即 printf("%.*lf", decimals, number).