Pages: [1]   Go Down
 Author Topic: Char to float conversion  (Read 1885 times) 0 Members and 1 Guest are viewing this topic.
Planet Earth
Offline
Newbie
Karma: 0
Posts: 36
I hope I didn't brain my damage...
 « on: April 03, 2011, 02:31:32 pm » Bigger Smaller Reset

Hey,

I have some data in a char array, lets say, char string[] = "231.067521"

I want to convert it to float and store in a variable (temp).

I tried atof but that yields only:  temp = 231.07

I need more no of digits, please suggest some efficient method for implementation in Arduino.

Thanks...
 Logged

0
Offline
Shannon Member
Karma: 117
Posts: 10112
:(){:|:&};: TOX id: fcb8e918bef08581e23f6ddf9d4dba77697c25b217bf372736ed959a95fde36df5b8c5b90fbb
 « Reply #1 on: April 03, 2011, 02:40:37 pm » Bigger Smaller Reset

this code works for flaot and negative number...
But is for serial, the input must be *a char:value
you can easily extrapolate the code you need

Code:
char input1;
char input2[10];
int indexInput2=0;
boolean inputValue=false;
boolean negativo=false;
/*
input data: *name:value\n
*/
while (Serial.available() > 0){
if (c =='*'){
indexInput2=0;
inputValue=false;
negativo=false;
//      Serial.println("starting input");
}else{
if (c =='\n'){
input2[indexInput2]='\0';
//        Serial.print("value: ");
//        Serial.println(input2);
int point=0, i=0;
float ris=0;
while(input2[i]!='\0'){
if (input2[i]=='.'){
point=indexInput2-i-1;
}else{
ris=(ris*10)+input2[i]-'0';
}
i++;
}
/*
Serial.print("ris: ");
Serial.println(ris);
Serial.print("point: ");
Serial.println(point);
*/
ris/=pow(10, point);
if (negativo){
ris*=-1;
}
executeCommand(input1, ris);
}else{
if (inputValue){
if (indexInput2==0 && c=='-'){
negativo = true;
}else{
input2[indexInput2]=c;
indexInput2++;
}
}else{
if (c ==':'){
//            Serial.println("command ok, waiting value");
inputValue=true;
}else{
input1=c;
}
}
}
}
}

}
 Logged

my Arduino code: https://github.com/lestofante/arduinoSketch
sei nuovo? non sai da dove partire? leggi qui: http://playground.arduino.cc/Italiano/Newbie

Seattle, WA USA
Offline
Brattain Member
Karma: 551
Posts: 46227
Seattle, WA USA
 « Reply #2 on: April 03, 2011, 06:17:49 pm » Bigger Smaller Reset

Quote
I tried atof but that yields only:  temp = 231.07
How do you know that atof truncated the data? I don't believe that it did. Prove it.

It's much more likely, to a near certainty in fact, that you caused the truncation when printing the value.
 Logged

Georgina Ontario
Offline
Sr. Member
Karma: 5
Posts: 437
Arduino rocks
 « Reply #3 on: April 03, 2011, 06:31:22 pm » Bigger Smaller Reset

Quote
I tried atof but that yields only:  temp = 231.07
How do you know that atof truncated the data? I don't believe that it did. Prove it.

It's much more likely, to a near certainty in fact, that you caused the truncation when printing the value.

Nullo Contendo!

No contest -- he's right!
 Logged

Just another Hacker

Planet Earth
Offline
Newbie
Karma: 0
Posts: 36
I hope I didn't brain my damage...
 « Reply #4 on: April 04, 2011, 07:44:22 am » Bigger Smaller Reset

hey Paul, I tried the following code, please let me know if it's my mistake...

Code:
char data[] = "231.065792";
float temp;

void setup()
{
Serial.begin(9600);
temp = atof (data);
Serial.println(temp);
}

void loop()
{
}

Quote
Output = 231.07
 Logged

Seattle, WA USA
Offline
Brattain Member
Karma: 551
Posts: 46227
Seattle, WA USA
 « Reply #5 on: April 04, 2011, 07:52:37 am » Bigger Smaller Reset

Quote
please let me know if it's my mistake...
It is. The Serial.print(ln)() function has optional arguments that influence how it outputs numbers as strings, for various kinds of numbers.

For floats, the 2nd argument defines how many decimal places to display. The default is 2. Hey, that matches what you see.

Serial.println(temp, 8); would have displayed different results.
 Logged

Planet Earth
Offline
Newbie
Karma: 0
Posts: 36
I hope I didn't brain my damage...
 « Reply #6 on: April 04, 2011, 08:07:49 am » Bigger Smaller Reset

hey, it works....thanks a lot!
 Logged

 Pages: [1]   Go Up