user-0 提问: 请教一个关于%g输出的问题

回复:
user: user-1
content: 希望对你有帮助：https://blog.csdn.net/it_xiangqiang/category_10581430.html 希望对你有帮助：https://blog.csdn.net/it_xiangqiang/category_10768339.html

user: user-2
content: 你看看？ 1）%g没有精度，默认为6；     g, G   The  double  argument  is  converted in style f or e (or F or E for G conversions).  The precision specifies the number of    significant digits.  If the precision is missing, 6 digits are given; if the precision is zero, it is treated as 1.  Style e is used if the exponent from its conversion is less than -4 or greater than or equal to the precision.  Trailing zeros  are removed from the fractional part of the result; a decimal point appears only if it is followed by at least one digit.  2）对于1.234567，选择%f格式；     g, G   The  double  argument  is  converted in style f or e (or F or E for G conversions).  The precision specifies the number of    significant digits.  If the precision is missing, 6 digits are given; if the precision is zero, it is treated as 1.  Style e is used if the exponent from its conversion is less than -4 or greater than or equal to the precision.  Trailing zeros  are removed from the fractional part of the result; a decimal point appears only if it is followed by at least one digit.  3）对于%g，精度指的是有效数字；     g, G   The  double  argument  is  converted in style f or e (or F or E for G conversions).  The precision specifies the number of    significant digits.  If the precision is missing, 6 digits are given; if the precision is zero, it is treated as 1.  Style e is used if the exponent from its conversion is less than -4 or greater than or equal to the precision.  Trailing zeros  are removed from the fractional part of the result; a decimal point appears only if it is followed by at least one digit.

user: user-3
content: include\float.h: …… #define DBL_DIG         15                      /* # of decimal digits of precision */ #define DBL_EPSILON     2.2204460492503131e-016 /* smallest such that 1.0+DBL_EPSILON != 1.0 */ #define DBL_MANT_DIG    53                      /* # of bits in mantissa */ #define DBL_MAX         1.7976931348623158e+308 /* max value */ #define DBL_MAX_10_EXP  308                     /* max decimal exponent */ #define DBL_MAX_EXP     1024                    /* max binary exponent */ #define DBL_MIN         2.2250738585072014e-308 /* min positive value */ #define DBL_MIN_10_EXP  (-307)                  /* min decimal exponent */ #define DBL_MIN_EXP     (-1021)                 /* min binary exponent */ #define _DBL_RADIX      2                       /* exponent radix */ #define _DBL_ROUNDS     1                       /* addition rounding: near */  #define FLT_DIG         6                       /* # of decimal digits of precision */ #define FLT_EPSILON     1.192092896e-07F        /* smallest such that 1.0+FLT_EPSILON != 1.0 */ #define FLT_GUARD       0 #define FLT_MANT_DIG    24                      /* # of bits in mantissa */ #define FLT_MAX         3.402823466e+38F        /* max value */ #define FLT_MAX_10_EXP  38                      /* max decimal exponent */ #define FLT_MAX_EXP     128                     /* max binary exponent */ #define FLT_MIN         1.175494351e-38F        /* min positive value */ #define FLT_MIN_10_EXP  (-37)                   /* min decimal exponent */ #define FLT_MIN_EXP     (-125)                  /* min binary exponent */ #define FLT_NORMALIZE   0 #define FLT_RADIX       2                       /* exponent radix */ #define FLT_ROUNDS      1                       /* addition rounding: near */ …… 浮点数格式 http://bbs.csdn.net/topics/390676437

user: user-4
content: e%和f%f中修饰符指定了小数点后的数字个数，比如%.6f表示保留6位小数 g%修饰符指定了显示的有效数字个数，比如%.6g表示保留6位有效数字

user: user-5
content: 建议查一下%f, %e, %g分别输出的格式是什么。 以下是linux系统上manpage对于printf的描述：可以了解一下如果看英文有点困难，可以去网上搜一下。   The conversion specifier        A character that specifies the type of conversion to be applied.  The conversion specifiers and their meanings are:         d, i   The  int  argument is converted to signed decimal notation.  The precision, if any, gives the minimum number of digits that must appear; if the converted value requires fewer digits, it               is padded on the left with zeros.  The default precision is 1.  When 0 is printed with an explicit precision 0, the output is empty.         o, u, x, X               The unsigned int argument is converted to unsigned octal (o), unsigned decimal (u), or unsigned hexadecimal (x and X) notation.  The letters abcdef are used for x conversions; the  let‐               ters  ABCDEF  are  used for X conversions.  The precision, if any, gives the minimum number of digits that must appear; if the converted value requires fewer digits, it is padded on the               left with zeros.  The default precision is 1.  When 0 is printed with an explicit precision 0, the output is empty.         e, E   The double argument is rounded and converted in the style [-]d.ddde±dd where there is one digit before the decimal-point character and the number of digits after it is equal to the pre‐               cision;  if the precision is missing, it is taken as 6; if the precision is zero, no decimal-point character appears.  An E conversion uses the letter E (rather than e) to introduce the               exponent.  The exponent always contains at least two digits; if the value is zero, the exponent is 00.         f, F   The double argument is rounded and converted to decimal notation in the style [-]ddd.ddd, where the number of digits after the decimal-point character is equal to the precision specifi‐               cation.   If the precision is missing, it is taken as 6; if the precision is explicitly zero, no decimal-point character appears.  If a decimal point appears, at least one digit appears               before it.                (SUSv2 does not know about F and says that character string representations for infinity and NaN may be made available.  SUSv3 adds a specification for F.  The  C99  standard  specifies               "[-]inf" or "[-]infinity" for infinity, and a string starting with "nan" for NaN, in the case of f conversion, and "[-]INF" or "[-]INFINITY" or "NAN*" in the case of F conversion.)         g, G   The  double  argument  is  converted in style f or e (or F or E for G conversions).  The precision specifies the number of significant digits.  If the precision is missing, 6 digits are               given; if the precision is zero, it is treated as 1.  Style e is used if the exponent from its conversion is less than -4 or greater than or equal to the precision.  Trailing zeros  are               removed from the fractional part of the result; a decimal point appears only if it is followed by at least one digit.         a, A   (C99;  not  in SUSv2, but added in SUSv3) For a conversion, the double argument is converted to hexadecimal notation (using the letters abcdef) in the style [-]0xh.hhhhp±; for A conver‐               sion the prefix 0X, the letters ABCDEF, and the exponent separator P is used.  There is one hexadecimal digit before the decimal point, and the number of digits after it is equal to the               precision.  The default precision suffices for an exact representation of the value if an exact representation in base 2 exists and otherwise is sufficiently large to distinguish values               of type double.  The digit before the decimal point is unspecified for nonnormalized numbers, and nonzero but otherwise unspecified for normalized numbers.         c      If no l modifier is present, the int argument is converted to an unsigned char, and the resulting character is written.  If an l modifier is present, the wint_t (wide  character)  argu‐               ment is converted to a multibyte sequence by a call to the wcrtomb(3) function, with a conversion state starting in the initial state, and the resulting multibyte string is written.         s      If no l modifier is present: The const char * argument is expected to be a pointer to an array of character type (pointer to a string).  Characters from the array are written up to (but               not including) a terminating null byte ('\0'); if a precision is specified, no more than the number specified are written.  If a precision is given, no null byte need be present; if the               precision is not specified, or is greater than the size of the array, the array must contain a terminating null byte.                If  an l modifier is present: The const wchar_t * argument is expected to be a pointer to an array of wide characters.  Wide characters from the array are converted to multibyte charac‐               ters (each by a call to the wcrtomb(3) function, with a conversion state starting in the initial state before the first wide character), up to and  including  a  terminating  null  wide               character.  The resulting multibyte characters are written up to (but not including) the terminating null byte.  If a precision is specified, no more bytes than the number specified are               written, but no partial multibyte characters are written.  Note that the precision determines the number of bytes written, not the number of wide characters or  screen  positions.   The               array must contain a terminating null wide character, unless a precision is given and it is so small that the number of bytes written exceeds it before the end of the array is reached.         C      (Not in C99 or C11, but in SUSv2, SUSv3, and SUSv4.)  Synonym for lc.  Don't use.         S      (Not in C99 or C11, but in SUSv2, SUSv3, and SUSv4.)  Synonym for ls.  Don't use.         p      The void * pointer argument is printed in hexadecimal (as if by %#x or %#lx).

user: user-6
content: http://en.cppreference.com/w/c/io/fprintf      converts floating-point number to decimal or decimal exponent notation depending on the value and the precision.   For the g conversion style conversion with style e or f will be performed. For the G conversion style conversion with style E or F will be performed. Let P equal the precision if nonzero, 6 if the precision is not specified, or 1 if the precision is ​0​. Then, if a conversion with style E would have an exponent of X:      if P > X ≥ −4, the conversion is with style f or F and precision P − 1 − X.     otherwise, the conversion is with style e or E and precision P − 1.   Unless alternative representation is requested the trailing zeros are removed, also the decimal point character is removed if no fractional part is left. For infinity and not-a-number conversion style see notes.

user: user-6
content: printf("%.7g", 1.234567f);