I am reading a string which i have to convert into a float with a maximum of precision.I didn't want to use toFloat() function since it make a truncation so i made my own code. My problem is that the final result is also approximated. I need a precision of 4 digits after the float.I read the documentation about float in the arduino website.They say it should be maximum 7 digits of precision but i don't get it.When i test my code on the value of '23,459' ,all i get is 23,46. Any suggestions?
void setup() {
Serial.begin(9600);
}
int longu=0;
char data='2';
String data2="23,459";
float val=0;
int posVir=0;
int t=0;
float postVir=0;
float preVir=0;
int lonPostVir=0;
int lonPreVir=0;
float posvir2=0;
void loop()
{
lonPostVir=(data2.substring(posVir+1)).length();
lonPreVir=(data2.substring(0,posVir)).length();
longu=data2.length();
posVir=data2.indexOf(",");
preVir=(data2.substring(0,posVir)).toFloat();
postVir=(data2.substring(posVir+1)).toFloat();
posvir2=postVir*pow(10,-lonPostVir);
val=preVir+postVir*pow(10,-lonPostVir);
Serial.println(val);
}
Serial.print
only gives two decimal points. (Otherwise it would return23.4589996337890625
as that is the closest float value that exists.)