Converting a floating point value to a string

 

Getting a number, like 421.4, to be a string is no small trick with the Arduino.

I found the code below and have used it in the Defect Detector code to "say" floating point values.

See:  http://www.arduino-hacks.com/float-to-string-float-to-character-array-arduino/

 

// based on info found here:  http://www.arduino-hacks.com/float-to-string-float-to-character-array-arduino/


void setup()
{
  Serial.begin(9600);
  
  float floatVal= 819.91 ;    // need to add non-zero digit at end to get right string-- strange!
  String stringVal = "";     
  
  stringVal+=String(int(floatVal))+ "."+String(getDecimal(floatVal)); //combining both whole and decimal part in string with a full                                                                      //stop between them
  Serial.print("stringVal: ");Serial.println(stringVal);              //display string value
  
  char charVal[stringVal.length()+1];                      //initialise character array to store the values
  stringVal.toCharArray(charVal,stringVal.length()+1);     //passing the value of the string to the character array
  
  Serial.print("charVal: ");  
  for(uint8_t i=0; i<sizeof(charVal);i++) Serial.print(charVal[i]); //display character array
  
}

void loop()
{
}


//function to extract decimal part of float
long getDecimal(float val)
{
 int intPart = int(val);
 long decPart = 10 *(val-intPart); //I am multiplying by 1000 assuming that the foat values will have a maximum of 3 decimal places
                                   //Change to match the number of decimal places you need
 if(decPart>0)return(decPart);           //return the decimal part of float number if it is available 
 else if(decPart<0)return((-1)*decPart); //if negative, multiply by -1
 else if(decPart=0)return(00);           //return 0 if decimal part of float number is not available
}