80. Convert a String to Float

#include <stdio.h>
#include <stdint.h>

float custom_atof(const char *str) {
    float result = 0.0f;
    float fraction = 0.0f;
    float divisor = 10.0f;
    int sign = 1;
    uint8_t i = 0;
    uint8_t in_fraction = 0;

    // Handle optional sign
    if (str[i] == '-') {
        sign = -1;
        i++;
    } else if (str[i] == '+') {
        i++;
    }

    while (str[i] != '\0') {
        if (str[i] == '.') {
            in_fraction = 1;
            i++;
            continue;
        }

        if (str[i] >= '0' && str[i] <= '9') {
            int digit = str[i] - '0';
            if (!in_fraction) {
                result = result * 10.0f + digit;
            } else {
                fraction += digit / divisor;
                divisor *= 10.0f;
            }
        } else {
            break;  // Stop if non-digit, non-dot char
        }
        i++;
    }

    return sign * (result + fraction);
}

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;
}

What’s the Goal?

Simulate atof() behavior: convert valid string like "123.45" to 123.45 as a float.

Why it matters in firmware?

  • Some embedded applications use floating-point sensors, telemetry, etc.
  • Minimal controller Compiler C libraries often lack atof()
  • Useful when parsing config strings or serial inputs with decimals

Solution Logic

  1. Handle optional sign
  2. Parse integer part normally (result = result * 10 + digit)
  3. After encountering ., switch to fractional parsing
  4. Use digit / divisor, and increment divisor by 10× per digit (e.g., 10, 100, 1000)
  5. Combine both integer and fractional values

     
Loading...

Input

123.45

Expected Output

123.45