C语言格式输出问题
C语言中格式输出小数点算一位么?比如printf("%4.3f"3.1415)输出的结果应该是3.142那么这3.142是4位还是五位??...
C语言中 格式输出 小数点算一位么?
比如 printf("%4.3f"3.1415) 输出的结果应该是3.142 那么这3.142 是4位 还是五位?? 展开
比如 printf("%4.3f"3.1415) 输出的结果应该是3.142 那么这3.142 是4位 还是五位?? 展开
展开全部
%4.3中的4描述的是printf的输出至少有4个字符,而你的3.142超出了4个字符,根据printf的规定,%4.3中的4就不起总用了,要服从于3.142的具体长度,为啥从1415变成142了呢?这时因为%4.3中,点后面的3描述的是小数后的精度,要三位,所以就是三位。兄弟,要多看MSDN。
参考自MSDN6.0:
printf Width Specification
The second optional field of the format specification is the width specification. The width argument is a nonnegative decimal integer controlling the minimum number of characters printed. If the number of characters in the output value is less than the specified width, blanks are added to the left or the right of the values — depending on whether the – flag (for left alignment) is specified — until the minimum width is reached. If width is prefixed with 0, zeros are added until the minimum width is reached (not useful for left-aligned numbers).
The width specification never causes a value to be truncated. If the number of characters in the output value is greater than the specified width, or if width is not given, all characters of the value are printed (subject to the precision specification).
If the width specification is an asterisk (*), an int argument from the argument list supplies the value. The width argument must precede the value being formatted in the argument list. A nonexistent or small field width does not cause the truncation of a field; if the result of a conversion is wider than the field width, the field expands to contain the conversion result.
参考自MSDN6.0:
printf Width Specification
The second optional field of the format specification is the width specification. The width argument is a nonnegative decimal integer controlling the minimum number of characters printed. If the number of characters in the output value is less than the specified width, blanks are added to the left or the right of the values — depending on whether the – flag (for left alignment) is specified — until the minimum width is reached. If width is prefixed with 0, zeros are added until the minimum width is reached (not useful for left-aligned numbers).
The width specification never causes a value to be truncated. If the number of characters in the output value is greater than the specified width, or if width is not given, all characters of the value are printed (subject to the precision specification).
If the width specification is an asterisk (*), an int argument from the argument list supplies the value. The width argument must precede the value being formatted in the argument list. A nonexistent or small field width does not cause the truncation of a field; if the result of a conversion is wider than the field width, the field expands to contain the conversion result.
展开全部
"%4.3f"
表示这个浮点数一共占4位。
小数点后3位。
小数点不算占位。
占位是对有效数字来说的。
表示这个浮点数一共占4位。
小数点后3位。
小数点不算占位。
占位是对有效数字来说的。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
小数点后几位 %4.3 打印后3位 只是你这样输不出
不记小数点
不记小数点
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询