-3

I want to set custom precision using printf()

like for example:

double pi = 3.14159265358979323846;
cin>>n;
cout<<setprecision(n)<<pi;

I want to implement this same functionality using printf()

fabian
  • 80,457
  • 12
  • 86
  • 114
Mohammad Farseen
  • 75
  • 1
  • 2
  • 8

1 Answers1

1

From printf:

(optional) . followed by integer number or *, or neither that specifies precision of the conversion. In the case when * is used, the precision is specified by an additional argument of type int, which appears before the argument to be converted, but after the argument supplying minimum field width if one is supplied.

So:

int n = ...;

printf("%.*f\n", n, pi);
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108