Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C: printf a float value

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)?

like image 452
Roman Rdgz Avatar asked Dec 01 '11 17:12

Roman Rdgz


People also ask

What do you use in printf () to display a floating point value?

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.

How do you Scanf a float?

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 .

What is the value of float in C?

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.


1 Answers

You can do it like this:

printf("%.6f", myFloat); 

6 represents the number of digits after the decimal separator.

like image 140
Roman Byshko Avatar answered Oct 12 '22 02:10

Roman Byshko