Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,907 questions

55,750 answers

573 users

How to extract a float from a string in C

3 Answers

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <regex.h>

int main() {
    const char* str = "The price is 148.95 dollars";
    const char* pattern = "[-+]?[0-9]*\\.?[0-9]+";

    regex_t regex;
    regmatch_t match[1];

    if (regcomp(&regex, pattern, REG_EXTENDED) != 0) {
        fprintf(stderr, "Failed to compile regex\n");
        return 1;
    }

    if (regexec(&regex, str, 1, match, 0) == 0) {
        char extracted[64];
        int length = match[0].rm_eo - match[0].rm_so;
        strncpy(extracted, str + match[0].rm_so, length);
        extracted[length] = '\0';

        float number = atof(extracted);
        printf("Extracted float: %.2f\n", number);
    }

    regfree(&regex);
    
    return 0;
}

  
  
/*
run:
  
Extracted float: 148.95
  
*/

 



answered Jul 28, 2025 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main() {
    const char* str = "The price is 148.95 dollars";
    char numberStr[64];
    int i = 0, j = 0;
    int decimalPointFound = 0;

    // Skip non-digit characters until a digit or dot is found
    while (str[i] != '\0') {
        if (isdigit(str[i]) || (str[i] == '.' && isdigit(str[i + 1]))) {
            // Start extracting the number
            while (isdigit(str[i]) || (str[i] == '.' && !decimalPointFound)) {
                if (str[i] == '.') decimalPointFound = 1;
                numberStr[j++] = str[i++];
            }
            break;
        }
        i++;
    }

    numberStr[j] = '\0'; // Null-terminate the extracted string

    if (j > 0) {
        float number = atof(numberStr);
        printf("Extracted float: %.2f\n", number);
    } else {
        printf("No number found.\n");
    }

    return 0;
}

  
  
/*
run:
  
Extracted float: 148.95
  
*/

 



answered Jul 28, 2025 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

// Function to extract first float from a string
float extractFloat(const char* str) {
    char numberStr[64];
    int i = 0, j = 0;
    int decimalPointFound = 0;

    while (str[i] != '\0') {
        if (isdigit(str[i]) || (str[i] == '.' && isdigit(str[i + 1]))) {
            while (isdigit(str[i]) || (str[i] == '.' && !decimalPointFound)) {
                if (str[i] == '.') decimalPointFound = 1;
                numberStr[j++] = str[i++];
            }
            break;
        }
        i++;
    }

    numberStr[j] = '\0';

    return (j > 0) ? atof(numberStr) : -1.0f;  // Return -1.0 if no number found
}

int main() {
    const char* str = "The price is 148.95 dollars";
    float number = extractFloat(str);

    if (number >= 0)
        printf("Extracted float: %.2f\n", number);
    else
        printf("No number found.\n");

    return 0;
}


  
  
/*
run:
  
Extracted float: 148.95
  
*/

 



answered Jul 28, 2025 by avibootz
...