String to float

Hi,
Regarding converting from String to float:

i've tried these:
char fb[10];
String tmpStr = "0.1234";
tmpStr.toCharArray(fb, 10);

atof(fb) -> gives 0.12
tmpStr.toFloat() -> gives 0.12. This is only available in Arduino 1.5.2 Beta. According to the author, it should return 6 decimals. i only get 2 decimals

The only thing that works is:

String(tmpStr.toFloat(),6) -> 0.1234
But, the result is back to String, where we first started! :slight_smile:

Any ideas?

hbx2013:
Hi,
Regarding converting from String to float:

i've tried these:
char fb[10];
String tmpStr = "0.1234";
tmpStr.toCharArray(fb, 10);

atof(fb) -> gives 0.12
tmpStr.toFloat() -> gives 0.12. This is only available in Arduino 1.5.2 Beta. According to the author, it should return 6 decimals. i only get 2 decimals

The only thing that works is:

String(tmpStr.toFloat(),6) -> 0.1234
But, the result is back to String, where we first started! :slight_smile:

Any ideas?

How are you viewing the value of the float?

p = 0.123456789;
Serial.begin(9600);
Serial.println(p, 6)

should return 0.123457 (six digits after the decimal).
Please look at this link: Serial.print() - Arduino Reference

Serial.print on a float defaults to two decimal places (yes that is rather annoying) so its nothing to
do with atof()

MarkT:
Serial.print on a float defaults to two decimal places (yes that is rather annoying) so its nothing to
do with atof()

oh, ok... so the conversion is correct. Great, didn't know that Serial.print was the bad guy here. Thanx alot! :slight_smile:

Great, didn't know that Serial.print was the bad guy here.

It's not. It's your incorrect usage that is the bad guy.

PaulS:

Great, didn't know that Serial.print was the bad guy here.

It's not. It's your incorrect usage that is the bad guy.

a bad guy, since he's not doing what i want it him do...without asking for it! He should be smarter than that! :slight_smile:

a bad guy, since he's not doing what i want it him do...without asking for it! He should be smarter than that!

That attitude is going to cause you all kinds of grief.

PaulS:

a bad guy, since he's not doing what i want it him do...without asking for it! He should be smarter than that!

That attitude is going to cause you all kinds of grief.

Well Paul... your lack of humor will most probably cause you a lot of grief as well. I cannot see how someone didn't realize that i was joking... !???