Precision modifier as a variable in C

In my program, the user enters the number of decimal places that he requires in the output. I save this entry as deci. How to use this deci variable as an accuracy modifier?

Example: Enter a number: 23.6788766 Input decimal places: 2 Output: 23.67

+4
source share
3 answers

If it is C, you can do:

float floatnumbervalue = 42.3456; int numberofdecimals = 2; printf("%.*f", numberofdecimals, floatnumbervalue); 
+12
source

In C, for example, to change the default precision of 6 digits to 8:

 int precision = 8; printf("%.*f\n", precision, 1.23456789); 

The precision argument must be of type int .

+3
source

You can use * modifiers, as shown in the following Wikipedia examples:

printf ("% * d", 5, 10) will print "10" with a total width of 5 characters, and printf ("%. * s", 3, "abcdef") will result in "abc".

http://en.wikipedia.org/wiki/Printf_format_string

+2
source

Source: https://habr.com/ru/post/1500211/


All Articles