How to combine Strings and other Variable Types

Can someone pleas explain,

I don’t understand why you can not simply combine a string variable with a floating point arithmetic variable (result) and result a composite string variable, like for example for output to a display or log file

like if I perform the calculation 5 / 3 = 1.666

and I want to append that variable to an existing string variable; I get a variable type miss-match error

I have goggled this several times and get a bunch of gobligook about a sprintf function I have no clue what they are talking about and loading up a million lib files that do not work

Seems like this would be a simple need for data logging, like if I wanted to create a .cdv file for import into excel
or to print voltage results to an lcd screen

can someone explain this in simple terms without cross referencing the entire known universe.

Thanks :wink:

This may help:
https://www.doc.ic.ac.uk/~wjk/C++Intro/RobMillerL2.html

One way to tackle this problem would be to include stdio as follows:

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

Unfortunately, the printf()/sprintf()/etc routines don't support floats, apparently because doing so bloats the resulting binary.

Instead, what you might do is roll your own function to handle such cases:

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

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

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

A better way, though, would be to call a particular avr-libc function (as noted in one of the threads linked above):

More information may be found here as well:

http://playground.arduino.cc/Main/Printf

Greensprings:
Can someone pleas explain,

I don’t understand why you can not simply combine a string variable with a floating point arithmetic variable (result) and result a composite string variable, like for example for output to a display or log file

like if I perform the calculation 5 / 3 = 1.666

and I want to append that variable to an existing string variable; I get a variable type miss-match error

I have goggled this several times and get a bunch of gobligook about a sprintf function I have no clue what they are talking about and loading up a million lib files that do not work

Seems like this would be a simple need for data logging, like if I wanted to create a .cdv file for import into excel
or to print voltage results to an lcd screen

can someone explain this in simple terms without cross referencing the entire known universe.

Thanks :wink:

While you can certainly mash a float into a string or a String, the question is "why would you want to do that?"

if you were data logging you probably wouldn't want to combine a string with a float and save it to a file that way, since you would only then have to handle that likewise on the other side when you are reading the data.

for example, if you were taking a machine's temperature (float) every hour, you would likely save the time and the value of the temperature and perhaps record the scale. I don't think you would put the three together and save it to a file.:

15:30:45580.76256765C where the time was 15:30:45 and the temperature was kept with 4 significant decimal digits

you would record it like this, perhaps:

15:30:45,580.7625,C

for easy parsing on the other side (into excel in your example)

that leaves your LCD printing, and again you don't have to put the two together to print them as if they were one:

lcd.print("My value is:");
lcd.print(myFloat);
lcd.println("F");

But I want to, that is my question,
for one, a TFT type display requires to pass a string variable

so i want to combine StringA + FloatB = StringC

"THE ANSWER IS" + 1.67 = "THE ANSWER IS 1.67"

How do you perform this " String Addition"?

A lot of people here will tell you to forget about the String class...it adds a lot of bloat to your programs. Instead, you character arrays and terminate them with the null character ('\0'). For example, run the program below and enter a floating point number via the Serial monitor to get an idea about using char arrays as strings. (Note there is a difference between String and string.) The code below converts the value to a float, but also just uses it as a string via the string copy (strcpy()) and string concatenate (strcat()) functions.

void setup() {

  Serial.begin(115200);
}

void loop() {
  char buff[25];
  char message[30];
  int charsRead;
  float val;
  
  if (Serial.available() > 0) {
    charsRead = Serial.readBytesUntil('\n', buff, 24);   // Get every thing up to the Enter key
    buff[charsRead] = '\0';                              // Make it a string
    val = atof(buff);                                    // Convert to a float
    Serial.print("The number entered was: ");
    Serial.println(val);                                 // Show as a numeric
    strcpy(message, "The number is ");                   // Copy some text
    strcat(message, buff);                               // Add the contents entered by user
    Serial.println("Or, you can print it as a string:");
    Serial.println(message);
  }
}

Are you using an old version of the IDE? Some of the operators may not exist in the old version of the API.

If you have two variables, a String and float, all that is needed is what you first expected:

String a = "THE ANSWER IS ";
float f = 1.23f;

String b = a + f;

Or just simply:

a += f;

The String library uses dynamic memory, and people get caught on large concatenations as temporaries will be created ( which also use dynamic memory ). Its easier ( for memory management ) to use c-strings, like econjacks example, however careful use of the String class can make it easy to do many things with text.

Greensprings:
"THE ANSWER IS 1.67"

Isn't this the simple way to make that appear

Serial.print("THE ANSWER IS ");
Serial.println(myFloatVar);

...R

so i want to combine StringA + FloatB = StringC

Why? The serial data is still going to be sent ONE CHARACTER AT A TIME, just like it would be if you used two or more print() calls.

There is just no way for the device on the other end to know whether you pissed away memory getting the data into one String/string before sending or saved resources and send the data one piece at a time.