Error when mulplying floats

Hi all.
I am puzzled about this issue.

the code below:

  
 float myfloat1=0.001;
 float myfloat2=0.1;
 float mytotal=myfloat1*myfloat2;
 Serial.println(String(mytotal));

returns a '0.00'. Instead of '0.0001

Can anyone explain this?
Regards

Because when you converted it to String you didn't tell it how many decimals to keep. The default is two.

Try this instead:

float myfloat1=0.001;
 float myfloat2=0.1;
 float mytotal=myfloat1*myfloat2;
 Serial.println(mytotal , 4);
2 Likes

Floats are similarly printed as ASCII digits, defaulting to two decimal places.

From:

Thank you.
regards

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.