help with string to float Function

HI There,, I have problem with string to float function, when I sent string value it give me integer value
could any one check the following code Please

String getPH() {    
  char phchar;
  while (phchar != '\r') {
    phchar = (char)Serial2.read();
    if (phchar != '\r') {
      phsensorstring += phchar;
    }
    
  }
  
  //call function convert string to float;
   
   pHfloat = StrToFloat(phsensorstring);
   Serial.println(phsensorstring);
   Serial.println(pHfloat);
 //this function to  convert strings to float
 float StrToFloat(String str){
  char carray[str.length() + 1]; //determine size of the array
  str.toCharArray(carray, sizeof(carray)); //put str into an array
  return atof(carray);
}

the result

phsensorstring
2.82

pHfloat
2

m-alamir:

   pHfloat = StrToFloat(phsensorstring);

You don't show how you've declared pHfloat, but if it was "int", you'd get the exact behaviour you describe.

yes.... You are correct. it is My mistake.
Thanks

  char phchar;
  while (phchar != '\r') {

Local variables are not initialized. You get, in the variable, whatever junk was left in the memory location it occupies. What is going to happen if that memory location did happen to contain '\r'?

You should NEVER write code like this. ALWAYS initialize local variables.