#include <stdio.h>
#include <stdint.h>
float custom_atof(const char *str) {
float res;
float res_int = 0.0f;
float res_frac = 0.0f;
float sign = 1.0f;
float divisor = 10.0f;
int i = 0;
int seen_decimal = 0;
if (str[0] == '-')
{
sign = -1.0f;
i++;
}
else if (str[0] == '+')
{
i++;
}
while (str[i] != '\0')
{
if (seen_decimal == 0 && str[i] == '.')
{
seen_decimal = 1;
i++;
continue;
}
if (seen_decimal)
{
res_frac += (str[i] - '0') / divisor;
divisor *= 10.0f;
}
else
{
res_int = res_int * 10.0f + (str[i] - '0');
}
i++;
}
res = sign * (res_int + res_frac);
return res;
}
int main() {
char str[101];
fgets(str, sizeof(str), stdin);
// Remove newline
uint8_t i = 0;
while (str[i]) {
if (str[i] == '\n') {
str[i] = '\0';
break;
}
i++;
}
float value = custom_atof(str);
printf("%.2f", value);
return 0;
}
Input
123.45
Expected Output
123.45