# Float number output

In C the syntax for formating a float number to print 3 digits after the decimal is `printf("%.3f", variable);`

The arduino doesn't seem to understand that syntax.

The arduino prints through the Serial object, that uses the serial/uart of the microchip.

You can see its output in the serial monitor.

``````void setup(){
Serial.begin(9600);
float x = 3.1415;
Serial.println(x);
}

void loop(){/*no loop*/}
``````

So how would you format it to print 3 digits? Is there a way to format the output?

When I put your code in it only prints the first 2 digits after the decimal point.

1. Multiply number by 1000 (shifts digits 3 places left)
2. Convert it into an integer (removing digits after decimal point)
3. Convert back to float
4. Divide by 1000 (moves digits 3 places right)

:)

Regarding your method Mike, I'm a bit cautious, it might not be quite so simple due to the fun of dealing with floating point numbers stored in a base 2 system, when us people like base 10.

Here's an example from python, which uses C doubles as it's default floating point type:

int(0.3 * 1000) / 1000.0 0.29999999999999999

In a normal environment one would just use snprintf: http://www.nongnu.org/avr-libc/user-manual/group__avr__stdio.html#g77070c245d4ca4f7ec7d7144260fb875

That function might be unsuitable or overkill for some reason, Mike's suggestion of multiplying by 1000 (but with differing later steps :P) might be one approach.

(One way (trunicating, not rounding) that comes to mind would be float x = 1.234; int y, z; y = x*1000; z = y % 1000; y =/ 1000; ...print y, print '.', print z