Question #359   Submitted by Answiki on 01/07/2021 at 07:09:20 PM UTC

In C, how to convert a floating-point number in a string to float ?

Answer   Submitted by Answiki on 01/07/2021 at 07:15:46 PM UTC

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.

14 events in history
Question by Answiki 05/13/2022 at 05:57:00 PM
In C, how to convert a char* to float?
Question by Answiki 10/10/2021 at 02:36:21 PM
How to convert a string to floating point in C?
Question by Answiki 10/10/2021 at 02:36:08 PM
In C, how to convert a string to floating point ?
Question by Answiki 01/07/2021 at 07:17:16 PM
In C, how to convert a ASCII floating-point number to double ?
Question by Answiki 01/07/2021 at 07:17:02 PM
In C, how to convert a floating-point number in a string to double ?
Question by Answiki 01/07/2021 at 07:16:52 PM
In C, how to convert a string to double ?
Question by Answiki 01/07/2021 at 07:16:37 PM
In C, how to convert a char* to double ?
Answer by Answiki on 01/07/2021 at 07:15:46 PM

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.

Answer by Answiki on 01/07/2021 at 07:11:41 PM

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.

Question by Answiki 01/07/2021 at 07:09:40 PM
In C, how to convert a ASCII floating-point number to float ?
Question by Answiki 01/07/2021 at 07:09:20 PM
In C, how to convert a floating-point number in a string to float ?
Question by Answiki 01/07/2021 at 07:08:19 PM
In C, how to convert a string to float ?
Answer by Answiki on 01/07/2021 at 07:08:08 PM

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.

Question by Answiki 01/07/2021 at 06:56:26 PM
In C, how to convert a char* to float ? replaced by #6131.
# ID Query URL Count

Icons proudly provided by Friconix.