A simple question.
I want to print a floating point number with precision given input from the user, i.e. for num=2.34567
and prec=2
, I should print 2.35
as the answer, and for prec=3
, I should print 2.346
. How can we achieve this? (prec is given input from the user during runtime).
Thanks in advance.
Asked
Active
Viewed 57 times
1

Govind Parmar
- 20,656
- 7
- 53
- 85

Akash
- 939
- 1
- 8
- 27
1 Answers
3
This is probably what you are looking for:
float num = 2.34567;
int prec = 3;
printf("%.*f", prec, num);

P.P
- 117,907
- 20
- 175
- 238