Use atof() or strtof()* instead:
printf("float value : %4.8f\n" ,atof(s));
printf("float value : %4.8f\n" ,strtof(s, NULL));
https://cplusplus.com/reference/cstdlib/atof/
https://cplusplus.com/reference/cstdlib/strtof/
atoll()is meant for integers.atof()/strtof()is for floats.
The reason why you only get 4.00 with atoll() is because it stops parsing when it finds the first non-digit.
*Note that strtof() requires C99 or C++11.
Use atof() or strtof()* instead:
printf("float value : %4.8f\n" ,atof(s));
printf("float value : %4.8f\n" ,strtof(s, NULL));
https://cplusplus.com/reference/cstdlib/atof/
https://cplusplus.com/reference/cstdlib/strtof/
atoll()is meant for integers.atof()/strtof()is for floats.
The reason why you only get 4.00 with atoll() is because it stops parsing when it finds the first non-digit.
*Note that strtof() requires C99 or C++11.
Unfortunately, there is no way to do this easily. Every solution has its drawbacks.
Use
atof()orstrtof()directly: this is what most people will tell you to do and it will work most of the time. However, if the program sets a locale or it uses a library that sets the locale (for instance, a graphics library that displays localised menus) and the user has their locale set to a language where the decimal separator is not.(such asfr_FRwhere the separator is,) these functions will stop parsing at the.and you will stil get4.0.Use
atof()orstrtof()but change the locale; it's a matter of callingsetlocale(LC_ALL|~LC_NUMERIC, "");before any call toatof()or the likes. The problem withsetlocaleis that it will be global to the process and you might interfer with the rest of the program. Note that you might query the current locale withsetlocale()and restore it after you're done.Write your own float parsing routine. This might be quite quick if you do not need advanced features such as exponent parsing or hexadecimal floats.
Also, note that the value 4.08 cannot be represented exactly as a float; the actual value you will get is 4.0799999237060546875.
Videos
If you can use C99 standard, then the best way is to use snprintf function. On first call you can pass it a zero-length (null) buffer and it will then return the length required to convert the floating-point value into a string. Then allocate the required memory according to what it returned and then convert safely.
This addresses the problem with sprintf that were discussed here.
Example:
Copyint len = snprintf(NULL, 0, "%f", amount);
char *result = malloc(len + 1);
snprintf(result, len + 1, "%f", amount);
// do stuff with result
free(result);
The second parameter is the format string after which the format arguments follow:
Copyfprintf(fPointer, "%f", amount);
%f tells fprintf to write this argument (amount) as string representation of the float value.
A list of possible format specifiers can (for example) be found here.
If you can use C99 standard, then the best way is to use snprintf function. On first call you can pass it a zero-length (null) buffer and it will then return the length required to convert the floating-point value into a string. Then allocate the required memory according to what it returned and then convert safely.
This addresses the problem with sprintf that were discussed here.
Example:
Copyint len = snprintf(NULL, 0, "%f", amount);
char *result = malloc(len + 1);
snprintf(result, len + 1, "%f", amount);
// do stuff with result
free(result);
The second parameter is the format string after which the format arguments follow:
Copyfprintf(fPointer, "%f", amount);
%f tells fprintf to write this argument (amount) as string representation of the float value.
A list of possible format specifiers can (for example) be found here.
I'm supposed to convert a string into a float without using any standard library function, and I can't figure out why this doesn't work.
float strToFloat(const char string[])
{
int i;
int j = 0;
int ten = 1;
float dec = 10;
float result = 0;
for (i = 0; string[i] != '\0'; ++i)
{
if (string[i] == '.')
{
for (; string[i] != '\0'; ++i)
{
dec = (dec * 10) + string[i] - '0';
++j;
}
}
else
result = (result * 10) + string[i] - '0';
}
for (; i != 0; --i)
{
ten *= 10;
}
dec /= ten;
printf("%d", dec);
result += dec;
return result;
}Could someone help me?
Ok so I want to convert a float or int to a string in C.
I want to do something like stringify in Javascript where it converts to a char of the number inserted. For example if the float or int was 20 it will equal "20." Not H or something like that.
How to do this in C?