printf传递给变量的精度为浮点数(小数位数)的浮点数 [英] printf a float value with precision (number of decimal digits) passed in a variable

查看:337
本文介绍了printf传递给变量的精度为浮点数(小数位数)的浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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;

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

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

我可以以编程方式编写printf的格式字符串,但是我想知道是否有更简单的方法来实现它.

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

推荐答案

使用".*"

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

示例:

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

精度

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

Precision

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

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

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.

宽度

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

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 precision is negative, the string will be left-aligned.

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

根据手册页

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

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.

这篇关于printf传递给变量的精度为浮点数(小数位数)的浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆