FmtDouble to Sprintf

I have a pH sensor that gives data out as a float.

To get the value to print on my TFT I used FmtDouble to go from float to char array.

Now I am trying to go from either the float or char array to sprintf.

float pHsensor; //Sensor Data
  --->FmtDouble---->char pHbuffer; //Conversion to char
        char pHbuffer------>sprintf------>webserver //Either char to sprint or....
        float pHsensor------>sprintf------>webserver //float to sprint.

I tried:
float-->sprintf|Got a long 5 digit negative number. When it should be ~7.0
Char-->sprintf|Got a 4 digit positive number. I am not sure if sprintf understands the fmtdouble char.
int--->sprintf|Doesn't show decimal because it is an integer.

Any ideas how?

I do not understand your question very well.

The pH sensor gives a string, representing a floating point number ?
And your sketch used a binary 'float' for the value ?

From string to float: atof()
From float to string: dtostrf()

When the 'stream' class is used (like for the Serial and Ethernet class), you can send a string or a 'float'. In that case you don't need a conversion.

There are a number of other options.
It is often possible to use an integer. I use sometimes an integer for centigrade for the temperature.
Suppose the temperature is 23.40 and the integer 'x' is 2340
I would use this: sprintf( buffer, "%d.%02d", (x/100), (x%100));

In order to save space, the Arduino *printf libraries are compiled without floating point support (so you can't do sprintf (buf, "%f", val)).

As has been mentioned, you need to use something like dtostrf or convert it to integer, dealing with the factional digits separately.

Note, in the AVR based systems, double internally is the same size as float (in the Due, double can hold larger numbers with more mantissa bits). In both the AVR and ARM based systems, the underlying hardware does not have floating point instructions, so every floating point operation is done by calling library functions.

Erdin:
I do not understand your question very well.

The pH sensor gives a string, representing a floating point number ?
And your sketch used a binary 'float' for the value ?

From string to float: atof()
From float to string: dtostrf()

When the 'stream' class is used (like for the Serial and Ethernet class), you can send a string or a 'float'. In that case you don't need a conversion.

There are a number of other options.
It is often possible to use an integer. I use sometimes an integer for centigrade for the temperature.
Suppose the temperature is 23.40 and the integer 'x' is 2340
I would use this: sprintf( buffer, "%d.%02d", (x/100), (x%100));

I should have actually asked a question, instead of saying "Any Idea how?" My question actually was, how can I convert a float into sprintf and have it retain the decimal? Since I am kind of OCD about having the decimal. I am probably going to treat the whole integers as X, and the fractional numbers as Y. Then just print each value individually.

dtostrf() just gave me the same values as when I used a float directly(~1683). I think it is trying to say 6.83, because that is my buffer solution at this point. Although if you have a way of going from float to sprintf() and retain the entire number(decimals included) I would love to hear it.

MichaelMeissner:
In order to save space, the Arduino *printf libraries are compiled without floating point support (so you can't do sprintf (buf, "%f", val)).

As has been mentioned, you need to use something like dtostrf or convert it to integer, dealing with the factional digits separately.

Note, in the AVR based systems, double internally is the same size as float (in the Due, double can hold larger numbers with more mantissa bits). In both the AVR and ARM based systems, the underlying hardware does not have floating point instructions, so every floating point operation is done by calling library functions.

Interesting. Thank you for this addition. Is there anyway to download a library for Arduino to allow printf libraries to compile floats?

Yeah, separate digits seems to make sense.

Can you give the code line that uses dtostrf() ?
It should (it must) work.

It is possible to add floating point to sprintf, but you have to change the system files of Arduino. Not something for beginners, not even something for moderate advanced users like myself.

MichaelMeissner:
In order to save space, the Arduino *printf libraries are compiled without floating point support (so you can't do sprintf (buf, "%f", val)).

Actually, there are multiple versions of the xxprintf() libraries that come
with the AVR gcc toolset that ships with the Arduino IDE.
Some have floating point and some don't.
The Arduino IDE uses the default libraries which have xxprintf() floating point support
stripped out in order to save space.
In order to use the other libraries requires changing an option to the linker.
The 1.0.x Arduino IDE does not allow setting linker options so there is no way
to get thelibraries that include xxprintf() floating point support when using the IDE
to build your images.

There are ways to do it, but it either involves not using the IDE for the build,
or renaming or modifying the default libC libraries so that when the IDE
uses the default libraries you end up tricking it to actually get different/modified versions.

Here is a thread about it:
http://forum.arduino.cc/index.php?topic=124809.0

--- bill

Erdin:
Can you give the code line that uses dtostrf() ?
It should (it must) work.

Below is a picture what it looks like (pH section):

Here is the parts of my code I am using:

char pHMaxChar[10]; //Buffer of 10 for conversion to array
char pHMinChar[10];
char pHChar[10];

float pHMin; //For pH Min
float pHMax; //For pH Max
float pHsensorValue; //For current pH
...
char CPbuffer[128];  //These are the areas that I apply sprintf() for my webserver.
char CPformat_buffer[64];
strcpy_P(CPformat_buffer,CP_code);
sprintf(CPbuffer,"document.getElementById(\"Current_pH\").innerHTML=%d;", pHChar);
client.println(CPbuffer);

char HPbuffer[128];
char HPformat_buffer[64];
strcpy_P(HPformat_buffer,HP_code);
sprintf(HPbuffer,"document.getElementById(\"High_pH\").innerHTML=%d;", pHMaxChar);
client.println(HPbuffer);

char LPbuffer[128];
char LPformat_buffer[64];
strcpy_P(LPformat_buffer,LP_code);
sprintf(LPbuffer,"document.getElementById(\"Low_pH\").innerHTML=%d;", pHMinChar);
client.println(LPbuffer);
...
dtostrf(pHMin, 2, 3, pHMinChar); //These are my dtostrf (float, XX., .YYY, array)
dtostrf(pHMax, 2, 3, pHMaxChar);
dtostrf(pHsensorValue, 2, 3,pHChar);

bperrybap:
Here is a thread about it:
Strange problem with "sprintf" - Programming Questions - Arduino Forum

--- bill

Thank you for this. It is interesting.

bump

You could try a few things.

The array of only 10 is small. Make it 16 or 20.
The width of only 2 is not a lot. Try 5.
How is the buffer printed to the client ?

What is the problem ? I can't read the screendump, it has been made smaller.

Erdin:
You could try a few things.

The array of only 10 is small. Make it 16 or 20.
The width of only 2 is not a lot. Try 5.
How is the buffer printed to the client ?

What is the problem ? I can't read the screendump, it has been made smaller.

It worked thank you for your diligence.