I've written a function that extracts double from a string. Like asfas123123afaf to 123123 or afafas12312.23131asfa to 12312.23131 using the point as a decimal separator.
Here is the code:
double get_double(const char *str, char sep)
{
char str_dbl[80];
size_t i,j;
char minus;
double dbl;
for (minus = 1, i = j = 0; i < strlen(str); ++i)
{
if (
(str[i] == '-' && minus)
|| (str[i] >= '0' && str[i] <= '9')
|| (str[i] == 'e' || str[i] == 'E')
)
{
str_dbl[j++] = str[i];
minus = 0;
}
}
str_dbl[j] = '\0';
dbl = strtod (str_dbl,NULL);
return dbl;
}
But now I want to set a user defined comma separator (char sep) from the ASCII-chars (without E or e that are literals for ^10). How could I implement it?
Let me sepcify this: We say the separator is ',' so the string is 123123asfsaf,adsd,as.1231 it should return 123123,1231 as a double. It recognizes the first ',' (from left) and ignore all other.
It is really hard for me to find a solution for this problem. I have thought about setlocale but I it doesn't seem the best solution.
Thank you!