In C, how to convert a string to floating point ?
In C / C++, atof()
is a dedicated function for converting strings to floats (or doubles). First, include stdlib.h
:
#include <stdlib.h>
Then, simply convert strings to floats (or doubles) :
char string[] = "10.66814";
float value = atof(string);
Test the previous code online on OneCompiler.
If the first sequence of non-whitespace characters in string does not form a valid floating-point number, the function atof()
returns 0.0
.
In C / C++, atof()
is a dedicated function for converting strings to floats (or doubles). First, include stdlib.h
:
#include <stdlib.h>
Then, simply convert strings to floats (or doubles) :
char string[] = "10.66814";
float value = atof(string);
Test the previous code online on OneCompiler.
If the first sequence of non-whitespace characters in string does not form a valid floating-point number, the function atof()
returns 0.0
.
In C / C++, there is a dedicated function for converting strings to doubles : atof()
. First, you'll need to include stdlib.h
:
#include <stdlib.h>
Then, simply convert string to double (or float) :
char string[] = "10.66814";
double value = atof(string);
Test the previous code online on OneCompiler.
If the first sequence of non-whitespace characters in string does not form a valid floating-point number, the function atof()
returns 0.0
.
In C / C++, there is a dedicated function for converting strings to floats : atof()
. First, you'll need to include stdlib.h
:
#include <stdlib.h>
Then, simply convert string to float :
char string[] = "10.66814";
float value = atof(string);
Test the previous code online on OneCompiler.
If the first sequence of non-whitespace characters in string does not form a valid floating-point number, the function atof()
returns 0.0
.
# | ID | Query | URL | Count |
---|