display or manipulate floating number and String

Hello everybody,

I would like to display floating numbers with the UTFT module. I think that the simple manipulation of these numbers is the same problem. I found two methods to display floating numbers :

float yy = 123.456;
char buf[32];
sprintf(buf, "%f", yy);
myGLCD.print(buf, CENTER, CENTER);

this compile but the result is the display of the caracter "?" (interrogation point).

second method :

float yy = 123.456;
String ss = String(yy);

the compliation gives an error : call of overloaded 'String(double&)' is ambiguous

Now, about string, If I code :

String ss = "Hello";
char buf[32];
sprintf(buf, "%s",ss);
myGLCD.print(buf, CENTER, CENTRE),

this compiles, at the execution, it seems that the sprintf hangs the program.

So, it appears that the sprintf does'nt work with float and String variables.

How do you do to display floating numbers ?

Thank you for your help.

Pierre

See here:

http://forum.arduino.cc/index.php/topic,148214.0.html

There's a small library (formatDouble) that turns a float into a string.

Thank you Nick Gammon for library.

But furthr, can you explain me why :

double dd = 100/3;
int ent = floor(dd); // or double ent = floor(dd); --> the result is the same
double frac = dd - ent;

gives 0 as a result ?

Thank you for your help.

Pierre

For enabling floats in sprintf (and sscanf) you can install this in \hardware\tools\avr\avr\lib (make a backup of that lib folder, and then extract and replace files): Arduino Forum

Another option is to use dtostrf (I believe it's more efficient, and use much less memory than sprintf with FP enabled): avr-libc: <stdlib.h>: General utilities

ChPr:
Thank you Nick Gammon for library.

But furthr, can you explain me why :

double dd = 100/3;

int ent = floor(dd); // or double ent = floor(dd); --> the result is the same
double frac = dd - ent;




gives 0 as a result ?

Thank you for your help.

Pierre

Simple... 100/3 is 33. The floor() of 33 is 33. Subtract 33 from 33 and you get 0.

Now, if you were working with floating point numbers, then it would be different.

double dd = 100.0/3.0; // Note the subtle difference here - the .0 forces it to be floating point, not integer.
double ent = floor(dd);
double frac = dd - ent;

There's also the dtostrf() and dtostre() functions to convert floats to strings, two different ways.

Je vous remercie tous pour vos remarques, liens et idées que vous m'avez apportés.

Cordialement.

Pierre

ChPr:
Je vous remercie tous pour vos remarques, liens et idées que vous m'avez apportés.

Cordialement.

Pierre

ChPr (via Google Translate):
Thank you all for your comments, links and ideas that you have given me.

Cordially.

Pierre

Sorry and thank you majenko for the translation :wink:

Pierre