Pages: [1]   Go Down
Author Topic: sprintf ?  (Read 16737 times)
0 Members and 1 Guest are viewing this topic.
Flen, Sweden
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-evil

I sure there is a simlpe answer.

/Bertil


Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25865
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Because it isn't implemented.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

NL
Offline Offline
Newbie
*
Karma: 1
Posts: 30
Tech Tinkerer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
  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) {};
  }
Logged

Dallas, TX USA
Online Online
Faraday Member
**
Karma: 63
Posts: 2649
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

--- bill
Logged

Flen, Sweden
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 smiley-grin
Tks, Bill - I work with Windoz 7.
It worked after some trial_error - "by hand".

/Bertil
Logged

Pages: [1]   Go Up
Jump to: