Splitting a string that contains a float value

Hi!

My goal is to use an Arduino to send processing multiple sensor readings and display them. Right now I am sending the values as a comma-separated string, and then plan on displaying each value individually by splitting the string apart.

When I send processing the test value of: 2085.46,5 my code only recognizes the second part (the 5) as a number and displays the first section (2085.46) as 0.

 inString = myPort.readStringUntil('\n');
    print(inString);
    inString = trim(inString);
    String[] splitstring = split(inString, ",");
    println(int(splitstring));

I am guessing this is because the first value is a float, however, I need those decimal places so I cannot have the Arduino simply convert it to an int.

I have looked at split tokens, but I honestly don’t understand how it is different nor do I know how to use it.

Lastly, as an afterthought, because I haven’t been able to try yet, can I call print(splitstring[0]); to print only the first value in the split string?

Hello,

Try this:

void setup() 
  {
  String inString = "2085.46,5"+ "\r\n"; // From Arduino Serial.println();
  print(inString);
  inString = trim(inString);
  print(inString);
  println();
  
  String[] splitstring1 = split(inString, ",");
  println(int(splitstring1));
  
  String[] splitstring2 = split(inString, ",");
  println(float(splitstring2));
  
  String[] splitstring3 = split(inString, ",");
  println(int(float(splitstring3)));
  }

Output:

The above is just an example.
I would rethink your code.

:)

1 Like