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
%f. a floating point number for floats. %u. int unsigned decimal. %e.
Float is a datatype which is used to represent the floating point numbers. It is a 32-bit IEEE 754 single precision floating point number ( 1-bit for the sign, 8-bit for exponent, 23*-bit for the value. It has 6 decimal digits of precision.
The format specifier is used during input and output. It is a way to tell the compiler what type of data is in a variable during taking input using scanf() or printing using printf(). Some examples are %c, %d, %f, etc.
%e %e represents the data in exponential power(scientific format), 'e' would be by default mean exponential power 10. %f %f represents the data in normal decimal form, upto six decimal places, although you can control, that upto how many decimal places did you want your output.
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);
printf("%.*f", n, c);
that will print out c with n places after the decimal.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With