I am working on a program that can concern a string into a number, such as 245.5. Unfortunately, I am unable to render the decimal part of the number.
Ive delcared my function and vairables as floats. The algorithm needs a little work, but I am specifically asking about getting a decimel value.
Code:
#include <stdio.h>
float strToInt (const char string[])
{
int i, negative, test;
float result = 0, remainder, intValue;
negative = 0;
remainder = 0;
test = 0;
if (string[0] == '-'){
negative = 1;
}
i = negative;
while ((string[i] >= '0' && string[i] <= '9') || string[i] == '.')
{
if (string[i] == '.' || test == 1){
test = 1;
remainder = (string[i+1]/100);
printf ("%f\n", remainder);
++i;
}
if (test == 0){
intValue = string[i] - '0';
result = result*10 + intValue;
++i;
}
}
if (negative == 1){
result = result * -1;
}
return result;
}
int main (void)
{
float strToInt (const char string[]);
printf ("%f\n", strToInt("245.5"));
printf ("%f\n", strToInt ("100")+ 25);
printf ("%f\n", strToInt("13x5"));
return 0;
}