1

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);
 } 
asked Apr 12, 2015 at 8:55
4
  • 3
    Serial.print only gives two decimal points. (Otherwise it would return 23.4589996337890625 as that is the closest float value that exists.) Commented Apr 12, 2015 at 15:14
  • Are you sure about that sir? Commented Apr 12, 2015 at 16:51
  • 1
    Sure about which of the two statements? Commented Apr 13, 2015 at 15:20
  • I confirm that 23.4589996337890625 (i.e. 12299272/2^19) is the float32 nearest to 23.459. Commented May 4, 2016 at 15:02

2 Answers 2

6

The simplest way to get more decimal places to print is this:

Serial.println(val,NUMBER_OF_DECMIAL_PLACES);

This will print out any number of digits after the decimal place. Regular print without the number of digits specified will default to 2 decimal places. for example, here is some code and the result that gets printed:

float val = 23.459;
Serial.println(val);
Serial.println(val, 4);
Serial.println(val, 10);

23.46

23.4590

23.4589996337

You may notice that with 10 decimal places printed you can start to see how much the actual value of 23.459 needs to be approximated to fit within a floating point number. This is similar to how 1/3 can not be represented 100% accurately in a finite number of decimal digits; 459/1000 cannot be represented 100% accurately in a finite number of binary bits, so it is approximated.

Also, note that for non-floating point numbers the optional second argument to print and println is the base it should be printed in (dec, hex, oct, ...) and has nothing to do with precision.

answered Apr 12, 2015 at 17:48
3

Just write the amount of numbers you want to get float here Serial.println(val); after the val. example: Serial.println(val, 5); It will give 5 digits after the decimal point

answered May 4, 2016 at 8:40

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.