I want to print a float value which has 2 integer digits and 6 decimal digits after the comma. If I just use printf("%f", myFloat)
I'm getting a truncated value.
I don't know if this always happens in C, or it's just because I'm using C for microcontrollers (CCS to be exact), but at the reference it tells that %f
get just that: a truncated float.
If my float is 44.556677
, I'm printing out "44.55"
, only the first two decimal digits.
So the question is... how can I print my 6 digits (and just the six of them, just in case I'm having zeros after that or something)?
we now see that the format specifier "%. 2f" tells the printf method to print a floating point value (the double, x, in this case) with 2 decimal places.
Read Float using Scanf() from User in C So, to read a float number from console, give the format and argument to scanf() function as shown in the following code snippet. float n; scanf("%f", n); Here, %f is the format to read a float value and this float value is stored in variable n .
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.
You can do it like this:
printf("%.6f", myFloat);
6 represents the number of digits after the decimal separator.
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