C语言 printf 一个带有精度(十进制位数)的浮点值传入变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35121029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:19:17  来源:igfitidea点击:

printf a float value with precision (number of decimal digits) passed in a variable

cfloating-pointprintfstring-formattingprecision

提问by Paolo

I can print a float value with precision of 3 decimal digits with

我可以打印精度为 3 个十进制数字的浮点值

double f = 1.23456;
printf( "%.3f", f );

Now I have the number of requested decimal digits that is not fixed but stored into a variable

现在我有请求的十进制数字的数量不是固定的但存储在一个变量中

int precision;
precision = atoi( argv[ 1 ] ); // ...just for example
double f = 1.23456;

How do I print the value of fwith the number of decimal digits specified in precision?

如何f使用 中指定的小数位数打印 的值precision

I can programmatically compose the format string for printfbut I'm wondering if there is a more simple way to do it.

我可以以编程方式编写格式字符串,printf但我想知道是否有更简单的方法来做到这一点。

回答by Hugo Rivera

Use ".*"

".*"

printf("%.*f", precision, f);

Example:

例子:

>>> printf("%.*f", 5, 3.14)
"3.14000"

Precision

精确

This is called the precision field. According to the man pages,

这称为精度字段。根据手册页

The precision: An optional precision, in the form of a period ('.') followed by an optional decimal digit string. Instead of a decimal digit string one may write "*" or "*m$" (for some decimal integer m) to specify that the precision is given in the next argument, or in the m-th argument, respectively, which must be of type int. If the precision is given as just '.', or the precision is negative, the precision is taken to be zero. This gives the minimum number of digits to appear for d, i, o, u, x, and X conversions, the number of digits to appear after the radix character for a, A, e, E, f, and F conversions, the maximum number of significant digits for g and G conversions, or the maximum number of characters to be printed from a string for s and S conversions.

精度:可选精度,形式为句点 ('.') 后跟可选的十进制数字字符串。代替十进制数字字符串,可以写“*”或“*m$”(对于某些十进​​制整数 m)来指定精度分别在下一个参数或第 m 个参数中给出,它们必须是类型为 int。如果精度仅以 '.' 形式给出,或者精度为负,则精度将为零。这给出了 d、i、o、u、x 和 X 转换出现的最小位数,a、A、e、E、f 和 F 转换的基数字符后出现的位数, g 和 G 转换的最大有效数字数,或 s 和 S 转换从字符串打印的最大字符数。

Width

宽度

The width field is similar and works for more formats, such as "%*s". Just remove the .. It will pad the output with spaces or zeros as needed. If precisionis negative, the string will be left-aligned.

width 字段类似,适用于更多格式,例如"%*s". 只需删除.. 它将根据需要用空格或零填充输出。如果precision是负数,字符串将左对齐。

>>> printf("%*s\n", 10, "left?")
"     left?"
>>> printf("%*s\n", -10, "left?")
"left?     "

According to the man pages,

根据手册页

The field width: An optional decimal digit string (with nonzero first digit) specifying a minimum field width. If the converted value has fewer characters than the field width, it will be padded with spaces on the left (or right, if the left-adjustment flag has been given). Instead of a decimal digit string one may write "*" or "*m$" (for some decimal integer m) to specify that the field width is given in the next argument, or in the m-th argument, respectively, which must be of type int. A negative field width is taken as a '-' flag followed by a positive field width.In no case does a nonexistent or small field width cause truncation of a field; if the result of a conversion is wider than the field width, the field is expanded to contain the conversion result.

字段宽度:指定最小字段宽度的可选十进制数字字符串(第一个数字非零)。如果转换后的值的字符数少于字段宽度,则会在左侧(或右侧,如果已给出左调整标志)填充空格。代替十进制数字字符串,可以写“*”或“*m$”(对于某些十进​​制整数 m)来指定字段宽度分别在下一个参数或第 m 个参数中给出,它们必须是 int 类型。负字段宽度被视为“-”标志,后跟正字段宽度。在任何情况下,不存在的或小的字段宽度都不会导致字段被截断;如果转换的结果比字段宽度宽,则扩展字段以包含转换结果。