在C动浮点格式说明符 [英] Dynamic Float Format Specifier in C

查看:98
本文介绍了在C动浮点格式说明符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法有一个用户inputed浮点格式说明?
例如,如果我打印本。

 浮法C = 15.0123
的printf(%2F。,C);//输出:15.01

我怎么可以指定小数数给一个变量?这样的:

  INT N = 3;
浮C = 15.0123
的printf(%(%I)楼,N,C);//输出:15.012


解决方案

在precision可以通过用星号 * 参数中指定。这就是所谓的论点提供的precision。

 浮法C = 15.0123;
INT M = 2;
的printf(%* F,M,C);

Is there any way to have a user inputed float format specifier? For example, if I print this.

float c = 15.0123
printf("%.2f", c);

// outputs: 15.01

How can I assign the number of decimal places to a variable? Like:

int n = 3;
float c = 15.0123
printf("%.(%i)f", n, c);

// outputs: 15.012

解决方案

The precision can be specified by an argument with the asterisk *. This is called an argument-supplied precision.

float c = 15.0123;
int m = 2;
printf("%.*f", m,  c);

这篇关于在C动浮点格式说明符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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