# convert char to double

how I can convert a 2.123 char to a double

If you use String you can use the toFloat() method:

``````String string_val;
float float_var;

...

float_var = string_val.toFloat();

...
``````

Do you really need a double?

this float func. will give me 1.12 I need it a double number 1.123

atof() to convert a null terminated character array (c-string).

need it a double number 1.123

``````float val = 1.123;
Serial.print(val, 3);
``````

I can print out to seven digits (at least...)

``````String
szStr = "1.123456789";
float
fVar;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while(!Serial);

}

void loop() {
// put your main code here, to run repeatedly:
fVar = szStr.toFloat();
Serial.println( fVar, 7 );
delay(500);
}
``````

okay thanks, guys
it works well I get the value at the serial monitor
but I wanna use it in an equation not just print it at serial

but I wanna use it in an equation not just print it at serial

Please post some MCVE test code (MCVE = minimal complete verifiable example) which demonstrates the problem you are having using a floating point value converted from a character string in an equation.

mohammedaly:
okay thanks, guys
it works well I get the value at the serial monitor
but I wanna use it in an equation not just print it at serial

Unless you have proof to the contrary, I think you'll find the internal representation has more than 2 digits of precision and will be useful for the math you're trying to do. What you're seeing is just how the print function works (i.e. rounding.)

An example of your code and the problem would help.

``````#include <SoftwareSerial.h>                           //we have to include the SoftwareSerial library, or else we can't use it
#define rx 2                                          //define what pin rx is going to be
#define tx 3                                          //define what pin tx is going to be

SoftwareSerial myserial(rx, tx);                      //define how the soft serial port is going to work

String inputstring = "";                              //a string to hold incoming data from the PC
String sensorstring = "";                             //a string to hold the data from the Atlas Scientific product
boolean input_string_complete = false;                //have we received all the data from the PC
boolean sensor_string_complete = false;               //have we received all the data from the Atlas Scientific product

void setup() {                                        //set up the hardware
Serial.begin(9600);                                 //set baud rate for the hardware serial port_0 to 9600
myserial.begin(9600);                               //set baud rate for the software serial port to 9600
inputstring.reserve(10);                            //set aside some bytes for receiving data from the PC
sensorstring.reserve(30);                           //set aside some bytes for receiving data from Atlas Scientific product
}

void serialEvent() {                                  //if the hardware serial port_0 receives a char
input_string_complete = true;                       //set the flag used to tell if we have received a completed string from the PC
}

void loop() {                                         //here we go...double x = std::stod(str);
if (input_string_complete) {                        //if a string from the PC has been received in its entirety
myserial.print(inputstring);                      //send that string to the Atlas Scientific product
myserial.print('\r');                             //add a <CR> to the end of the string
inputstring = "";                                 //clear the string
input_string_complete = false;                    //reset the flag used to tell if we have received a completed string from the PC
}

if (myserial.available() > 0) {                     //if we see that the Atlas Scientific product has sent a character
sensorstring += inchar;                           //add the char to the var called sensorstring
if (inchar == '\r') {                             //if the incoming character is a <CR>
sensor_string_complete = true;                  //set the flag
}
}

if (sensor_string_complete == true) {               //if a string from the Atlas Scientific product has been received in its entirety
if (isdigit(sensorstring) == false) {          //if the first character in the string is a digit
Serial.println(sensorstring);                   //send that string to the PC's serial monitor
}
else                                              //if the first character in the string is NOT a digit
{
print_RGB_data();                               //then call this function
}
sensorstring = "";                                //clear the string
sensor_string_complete = false;                   //reset the flag used to tell if we have received a completed string from the Atlas Scientific product
}
}

void print_RGB_data(void) {                           //this function will pars the string

char sensorstring_array;                        //we make a char array
char *red;                                          //char pointer used in string parsing
char *grn;                                          //char pointer used in string parsing
char *blu;
char *x; //char pointer used in string parsing
char *y;
char *Y;

sensorstring.toCharArray(sensorstring_array, 30);   //convert the string to a char array
red = strtok(sensorstring_array, ",");              //let's pars the array at each comma
grn = strtok(NULL, ",");                            //let's pars the array at each comma
blu = strtok(NULL, ",");                            //let's pars the array at each comma
x = strtok(NULL, ",");
y = strtok(NULL, ",");
Y = strtok(NULL, ",");

Serial.print("  R:");
Serial.print(red);

Serial.print("  G:");
Serial.print(grn);

Serial.print("  B:");
Serial.print(blu);

int r= atoi(red);
int g= atoi(grn);
int b= atoi(blu);
float xx= atof(x);
float yy= atof(y);
int Y= atoi(Y);

double k = (xx+yy)*0.124;

}
``````

i have a ezo color sensor it gives RGB and xyY values I wanna use x and y in a simple equation as shown in the code but i cant because values must be converted into double
and i need 3 digits after the point so i cant use float it gives only two digits

``````String string_val;
float float_var;

...

// convert to float
float_var = string_val.toFloat();
// and print
Serial.print("float_val = "); Serial.println(float_val, 3);
...

float_val *=2 ;
Serial.print("float_val = "); Serial.println(float_val, 3);
``````

Note 1)
Serial.print just gives a human readable interpretation of the float; by default it only prints 2 digits after the decimal dot. The underlaying float is still has the value 2.123.

Note 2)
The use of String (capital S) can cause issues at run time due to holes in memory. You can read the updated Serial Input Basics thread to get ideas for a different approach. It also contains an example using atoi() to convert text to an integer; instead of atoi(), you can use atof() to convert text to a float.

I wanna use x and y in a simple equation as shown in the code but i cant because values must be converted into double
and i need 3 digits after the point so i cant use float it gives only two digits

``````float xx= atof(x);
float yy= atof(y);
double k = (xx+yy)*0.124;
``````

What Arduino are you using? On the Uno and other ATMEGA based boards, a double occupies 4 bytes. That is, the double implementation is exactly the same as the float, with no gain in precision.

Floats have only 6-7 decimal digits of precision. That means the total number of digits, not the number to the right of the decimal point. Unlike other platforms, where you can get more precision by using a double (e.g. up to 15 digits), on the Arduino, double is the same size as float.

What evidence do you have that the values returned by atof() are limited to two digits after the decimal point?

Please print out and post the data you wish to parse and convert which is received in sensorstring_array.

``````sensorstring.toCharArray(sensorstring_array, 30);   //convert the string to a char array
``````

From my reading of the data sheet for the EZO RGB sensor, the xyY values for the CIE 1931 color space are of the form 0.xxx and 0.yyy and int YYY.