Avoid trailing zeroes in printf()

前端 未结 14 2337
猫巷女王i
猫巷女王i 2020-11-22 07:18

I keep stumbling on the format specifiers for the printf() family of functions. What I want is to be able to print a double (or float) with a maximum given number of digits

14条回答
  •  长情又很酷
    2020-11-22 07:51

    Slight variation on above:

    1. Eliminates period for case (10000.0).
    2. Breaks after first period is processed.

    Code here:

    void EliminateTrailingFloatZeros(char *iValue)
    {
      char *p = 0;
      for(p=iValue; *p; ++p) {
        if('.' == *p) {
          while(*++p);
          while('0'==*--p) *p = '\0';
          if(*p == '.') *p = '\0';
          break;
        }
      }
    }
    

    It still has potential for overflow, so be careful ;P

提交回复
热议问题