Arduino Forum

Using Arduino => Programming Questions => Topic started by: Bertil_Lindstrom on Oct 06, 2012, 09:41 pm

Title: sprintf ?
Post by: Bertil_Lindstrom on Oct 06, 2012, 09:41 pm
Hello !
I am new in C++ and Arduino.

I want to use sprintf for make a string for displying on a LED-display.
But I have problems with the "format" string.
I have tried "sprintf(A_ans, "%02d %02d %02.2f",I_grader,I_min,F_sec);"
The first two numbers is ok, "%02.2f" is giving me hard time ]:)

I sure there is a simlpe answer.

/Bertil


Title: Re: sprintf ?
Post by: AWOL on Oct 06, 2012, 09:41 pm
Because it isn't implemented.
Title: Re: sprintf ?
Post by: DeepZ on Oct 07, 2012, 01:35 am
As AWOL indicated, the stdlib (i think it is stdlib) linked in by Arduino does not support floats in the sprintf.
One way around this would be to tinker with Arduino IDE to include the full/extended stdlib to get the float working in sprintf.
Another way is to convert the float using :
    dtostrf(floatVar, minStringWidthIncDecimalPoint, numVarsAfterDecimal, charBuf);

Example :
Code: [Select]

  void setup() {
    Serial.begin(19200);
  }

  void loop() {
    float F = 1.25;
    char buffer[16];
 
    dtostrf(F,5,2,buffer);
    Serial.print("Float : ");
    Serial.println(buffer);
 
    //Do not loop
    while(true) {};
  }
Title: Re: sprintf ?
Post by: bperrybap on Oct 07, 2012, 03:30 am

Because it isn't implemented.


Actually it is implemented in the AVR gcc toolset. It is just that the sprintf() provided by default in the AVR libc.a
does not have floating point support (to keep things smaller)
and the Arduino IDE does not allow setting the needed linker options to
to use the floating point version.

See my comments in this thread for full details and how to work around it:
http://arduino.cc/forum/index.php/topic,124809.0.html (http://arduino.cc/forum/index.php/topic,124809.0.html)

--- bill
Title: Re: sprintf ?
Post by: Bertil_Lindstrom on Oct 07, 2012, 09:33 pm
:D
Tks, Bill - I work with Windoz 7.
It worked after some trial_error - "by hand".

/Bertil
Title: Re: sprintf ?
Post by: bhavasarbhagyesh on Jan 29, 2016, 06:36 am
 HEY GUYS I GOT THE ANSWER FOR, CONVERT FLOATING VALUES IN STRING USING SPRINTF(); FUNCTION   


                                      double temp = 55.55;
                      char buffer[4];

                  sprintf(buffer, "%02.02f", temp);

                  ssd1306_set_cursor(15, 26);
                  ssd1306_putstring(buffer);
                       ssd1306_display();

OUTPUT:
55.55
Title: Re: sprintf ?
Post by: OldSteve on Jan 29, 2016, 10:39 am
HEY GUYS I GOT THE ANSWER FOR, CONVERT FLOATING VALUES IN STRING USING SPRINTF(); FUNCTION   


                                      double temp = 55.55;
                      char buffer[4];

                  sprintf(buffer, "%02.02f", temp);

                  ssd1306_set_cursor(15, 26);
                  ssd1306_putstring(buffer);
                       ssd1306_display();

OUTPUT:
55.55
You think? You declare a 4 character array, then expect 6 characters (including the terminating null) to fit in it, for one.

And why does this print "?":-
Code: [Select]
void setup()
{
    Serial.begin(115200);
    double temp = 55.55;
    char buffer[4];
    sprintf(buffer, "%02.02f", temp);
    Serial.println(buffer);                  // Prints "?"
}

void loop(){}


Also why does this print "2":-
Code: [Select]
void setup()
{
    Serial.begin(115200);
    double temp = 55.55;
    char buffer[4];
    sprintf(buffer, "%02.02f", temp);
    Serial.println(strlen(buffer));        // Prints "2"
}

void loop(){}
Title: Re: sprintf ?
Post by: sterretje on Jan 29, 2016, 03:56 pm
And all of that three years after the opening post :)
Title: Re: sprintf ?
Post by: OldSteve on Jan 29, 2016, 05:45 pm
And all of that three years after the opening post :)
Ha. I didn't notice that. Well it took a while, but at least he got the answer. :D