I am struggling to get sprintf to do what I want. I have an integer that I want to display as a number with 1 decimal place and up to 4 digits in front of the decimal point and leading spaces. So for example if the number is 1234, I want to display it as " 123.4"
I have tried a loop that increments the number by one and the outputs the result, the code to output to the console is
Serial.print(F("trip miles "));
Serial.print(trip_miles);
Serial.print(F(" trip actual "));
Serial.println(sprintf("%.1f", trip_miles));
This one of many versions of the sprintf I have tried, they all give the same result which is
trip mile 0 trip actual 1
trip mile 1 trip actual 0
trip mile 2 trip actual 0
trip mile 3 trip actual 4
trip mile 4 trip actual 3
trip mile 5 trip actual 2
trip mile 6 trip actual 1
trip mile 7 trip actual 0
trip mile 8 trip actual 5
trip mile 9 trip actual 4
trip mile 10 trip actual 3
trip mile 11 trip actual 2
Which arduino are you using? Many do not support float in sprintf. You can use dtostrf() instead, or convert the float to an integer first and then use sprintf (or multiple calls to print - more code for you, but takes less code than sprintf if that is the only place it is used).
@mgb, please tell us which sprintf() your are using, what library does it come with, how did you install it, with links and a full working sketch.
Are you sure that you have a classic Arduino Nano with ATmega328P microcontroller ?