This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Printing floating point numbers to UART

Hey everyone,

I'm trying to print floating point numbers to the UART to debug my application. I know the nrf51 is doing the operations correctly because if I convert the variable to an int I can print it correctly and I get (~)accurate values. When I try to print floats nothing happens. I read that this may be because I'm linking against a library (for sprintf) that has no floating point support.

Any workarounds?

Parents
  • Either link with floating point support. newlib-nano has that option or you do it manually using integer. Cast to int to printf integer part, then take the floating part multiply by 10, 100, 1000 depending on how many digits you want to keep. Don't forget to add .5 to round up. Then print the int part again. For example 2 decimal points : multiply by 100.0 and printf format is "%d.%02d", for 4 decimal point : multiply by 10000.0 using format "%d.%04d".

Reply
  • Either link with floating point support. newlib-nano has that option or you do it manually using integer. Cast to int to printf integer part, then take the floating part multiply by 10, 100, 1000 depending on how many digits you want to keep. Don't forget to add .5 to round up. Then print the int part again. For example 2 decimal points : multiply by 100.0 and printf format is "%d.%02d", for 4 decimal point : multiply by 10000.0 using format "%d.%04d".

Children
Related