Very strange sprintf behavior

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

How and where is trip_miles declared ?
Please post a complete sketch that illustrates the problem

Sprintf returns the number of characters output.
It also outputs to a buffer, which you appear not to have provided.

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

trip_miles is declared in the body of the code with int trip_miles = 0;

The spec for sprintf that I have is

sprintf(format,arg1,arg2,arg++) 

There is no reference to a buffer

It is an int. Why are you trying to print it with a decimal place? Do you know what an int is?

I am using the nano, trip_miles is an integer, I am trying to add the decimal point with the sprintf

Then it's wrong. Can you post a link to where you found that spec? That's the spec for printf, not sprintf.

You're confusing sprintf with printf.
sprintf outputs to a buffer, printf to stdout.
Both return the number of characters output.

i don't believe the "%f" format is support on Arduino. need to use dtostrf()

Try

  Serial.print(F("trip miles "));
  Serial.print(trip_miles);
  Serial.print(F(" trip actual "));
  Serial.print(trip_miles));
  Serial.ptintln(F(".0"));

Is your code written in PHP?

This would just put a .0 at the end of the output, if the number is 1234, I am trying to output 123.4

No, C

So why show the sprintf reference for PHP?

int odometer = 1234;
char buffer [20];
sprintf (buffer, "%d.%d", odometer /10, odometer % 10);
1 Like

oops, thanks for that, I'm using PHP for something else - back to the drawing board.

Thanks everyone for your help

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

Either use dtostrf, or convert the float to an integer in 10's and print from that.