How to fix mbed printf() ignoring decimals in PlatformIO
Problem:
You are using code like
printf("%.2f\n", myFloat);
in your mbed/PlatformIO application, but instead of printing myFloat
with 2 decimal places, it always prints it with 6 decimal places (like 0.000000
).
Solution
mbed uses the minimal-printf
library by default which is configured to save space on the Microcontroller. Hence, float max decimals support is disabled by default. In order to get all printf
features at the expense of more flash usage and much slower executing, us add mbed_app.json
in the root directory of the PlatformIO project with "target.printf_lib": "std"
:
{
"target_overrides": {
"*": {
"target.printf_lib": "std"
}
}
}
See the platform configuration option page for more details and similar options.