Validating a string which represents a double ???????

Hi guys !
I have a C string, and i need to convert it to a double.
I know i can use function atof()
**** BUT **********
I need to know whether or not the string represents
a VALID double ! (e.g. "8.112" is a valid double
whereas "8.aa2" is not).

Does anybody know how to make sure that the string
is a valid double ??????

Thanx a bunch for any suggestions !

-Kaylie
«1

Comments

  • [code]
    INVALID_STRING = FALSE
    FOR I = 0; I < LENGTH OF STRING; I++
    IF STRING[I] < '0' || STRING[I] > '9'
    IF STRING[I] != '.'
    INVALID_STRING = TRUE
    BREAK
    END IF
    END IF
    END FOR

    [/code]

    : Hi guys !
    : I have a C string, and i need to convert it to a double.
    : I know i can use function atof()
    : **** BUT **********
    : I need to know whether or not the string represents
    : a VALID double ! (e.g. "8.112" is a valid double
    : whereas "8.aa2" is not).
    :
    : Does anybody know how to make sure that the string
    : is a valid double ??????
    :
    : Thanx a bunch for any suggestions !
    :
    : -Kaylie
    :


  • Humm...!
    Thank you so much for the quick reply !
    :0)



    : [code]
    : INVALID_STRING = FALSE
    : FOR I = 0; I < LENGTH OF STRING; I++
    : IF STRING[I] < '0' || STRING[I] > '9'
    : IF STRING[I] != '.'
    : INVALID_STRING = TRUE
    : BREAK
    : END IF
    : END IF
    : END FOR
    :
    : [/code]
    :
    : : Hi guys !
    : : I have a C string, and i need to convert it to a double.
    : : I know i can use function atof()
    : : **** BUT **********
    : : I need to know whether or not the string represents
    : : a VALID double ! (e.g. "8.112" is a valid double
    : : whereas "8.aa2" is not).
    : :
    : : Does anybody know how to make sure that the string
    : : is a valid double ??????
    : :
    : : Thanx a bunch for any suggestions !
    : :
    : : -Kaylie
    : :
    :
    :

  • also want to check for the presence of more than one period. "12...45" is not valid either.
    :
    :
    : : [code]
    : : INVALID_STRING = [red]TRUE[/red]
    : : FOR I = 0; I < LENGTH OF STRING; I++
    : : IF STRING[I] < '0' || STRING[I] > '9'
    : : IF STRING[I] != '.'
    : : INVALID_STRING = [red]FALSE[/red]
    : : BREAK
    : : END IF
    : : END IF
    : : END FOR
    : :
    : : [/code]
    : :

  • : also want to check for the presence of more than one period. "12...45" is not valid either.
    : :
    : :
    : : : [code]
    : : : INVALID_STRING = [blue]FALSE // Assume string is valid[/blue]
    : : : FOR I = 0; I < LENGTH OF STRING; I++
    : : : IF STRING[I] < '0' || STRING[I] > '9'
    [blue]This character is not a digit[/blue]
    : : : IF STRING[I] != '.'
    [blue]The period is the only valid non-digit character[/blue]
    : : : INVALID_STRING = [blue]TRUE[/blue]
    : : : BREAK
    : : : END IF
    : : : END IF
    : : : END FOR
    : : :
    : : : [/code]
    : : :
    :
    :

  • How about this?


    char doubleStr[n] = ...
    char otherStr[n];

    sprintf(otherStr,"%f",atof(doubleStr));

    if(strcmp(doubleStr,otherStr)==0)

  • : How about this?
    :
    :
    : char doubleStr[n] = ...
    : char otherStr[n];
    :
    : sprintf(otherStr,"%f",atof(doubleStr));
    :
    : if(strcmp(doubleStr,otherStr)==0)
    :
    :

    How about:

    [code]
    if(sscanf(dblStr, "%f", &dblVal) != 1)
    {
    printf("Not a double!
    ");
    }
    else
    {
    printf("I don't care even if it's a double ;-)
    ");
    }
    [/code]
  • : : How about this?
    : :
    : :
    : : char doubleStr[n] = ...
    : : char otherStr[n];
    : :
    : : sprintf(otherStr,"%f",atof(doubleStr));
    : :
    : : if(strcmp(doubleStr,otherStr)==0)
    : :
    : :
    :
    : How about:
    :
    : [code]
    : if(sscanf(dblStr, "%f", &dblVal) != 1)
    : {
    : printf("Not a double!
    ");
    : }
    : else
    : {
    : printf("I don't care even if it's a double ;-)
    ");
    : }
    : [/code]
    :


    That is the the same code as atof(), just alot more overhead. It will not recognize if there are junk letters at the end of the string.

  • : : : How about this?
    : : :
    : : :
    : : : char doubleStr[n] = ...
    : : : char otherStr[n];
    : : :
    : : : sprintf(otherStr,"%f",atof(doubleStr));
    : : :
    : : : if(strcmp(doubleStr,otherStr)==0)
    : : :
    : : :
    : :
    : : How about:
    : :
    : : [code]
    : : if(sscanf(dblStr, "%f", &dblVal) != 1)
    : : {
    : : printf("Not a double!
    ");
    : : }
    : : else
    : : {
    : : printf("I don't care even if it's a double
    ");
    : : }
    : : [/code]
    : :
    :
    :
    : That is the the same code as atof(), just alot more overhead. It will not recognize if there are junk letters at the end of the string.
    :
    :

    sscanf() approach should be of lesser overhead when compared to the
    other one that involves atof(), sprintf(), and strcmp().

    As for the case where there're junk letters, that's how errors can
    be detected which atoi() is not capable of.
  • : : That is the the same code as atof(), just alot more overhead. It will not recognize if there are junk letters at the end of the string.
    : :
    : :
    :
    : sscanf() approach should be of lesser overhead when compared to the
    : other one that involves atof(), sprintf(), and strcmp().

    [blue]Naturally, sence it has more functionality. If there is no need to detect junk letters, then use atof().[/blue]

    : As for the case where there're junk letters, that's how errors can
    : be detected which atoi() is not capable of.
    :

    [blue]
    Huh? What makes sscanf() better than atof() ?

    If you have the string "123.45JUNK" then both the atof() and the sscanf() way will return 123.45.

    If you have the string "JUNK123.45" then both functions will fail.
    So why use sscanf()?
    [/blue]
  • : : : That is the the same code as atof(), just alot more overhead. It will not recognize if there are junk letters at the end of the string.
    : : :
    : : :
    : :
    : : sscanf() approach should be of lesser overhead when compared to the
    : : other one that involves atof(), sprintf(), and strcmp().
    :
    : [blue]Naturally, sence it has more functionality. If there is no need to detect junk letters, then use atof().[/blue]
    :
    : : As for the case where there're junk letters, that's how errors can
    : : be detected which atoi() is not capable of.
    : :
    :
    : [blue]
    : Huh? What makes sscanf() better than atof() ?
    :
    : If you have the string "123.45JUNK" then both the atof() and the sscanf() way will return 123.45.
    :
    : If you have the string "JUNK123.45" then both functions will fail.
    : So why use sscanf()?
    : [/blue]
    :

    Ok, I take that back. I was incorrectly thinking that scanf() family
    uses space as a delimiter while reading the input. However, sscanf()
    approach for the string-to-numeric (int or float or double) conversion
    is a neat one.
  • you're right -- I misread "INVALID_STRING" as "VALID_STRING"

  • : : : : That is the the same code as atof(), just alot more overhead. It will not recognize if there are junk letters at the end of the string.
    : : : :
    : : : :
    : : :
    : : : sscanf() approach should be of lesser overhead when compared to the
    : : : other one that involves atof(), sprintf(), and strcmp().
    : :
    : : [blue]Naturally, sence it has more functionality. If there is no need to detect junk letters, then use atof().[/blue]
    : :
    : : : As for the case where there're junk letters, that's how errors can
    : : : be detected which atoi() is not capable of.
    : : :
    : :
    : : [blue]
    : : Huh? What makes sscanf() better than atof() ?
    : :
    : : If you have the string "123.45JUNK" then both the atof() and the sscanf() way will return 123.45.
    : :
    : : If you have the string "JUNK123.45" then both functions will fail.
    : : So why use sscanf()?
    : : [/blue]
    : :
    :
    : Ok, I take that back. I was incorrectly thinking that scanf() family
    : uses space as a delimiter while reading the input. However, sscanf()
    : approach for the string-to-numeric (int or float or double) conversion
    : is a neat one.
    :


    What sscanf() does after it's done parsing is to call the strtof(), strtol() etc functions. The same is done from the atof/atoi functions, with base 10 as default.

  • :
    : What sscanf() does after it's done parsing is to call the strtof(), strtol() etc functions. The same is done from the atof/atoi functions, with base 10 as default.
    :

    sscanf() approach is a much neater and shorter solution to verify if
    a given string is double or not compared to the one that converts the
    string to float, prints it into a string, and then compares the two
    strings.
  • : :
    : : What sscanf() does after it's done parsing is to call the strtof(), strtol() etc functions. The same is done from the atof/atoi functions, with base 10 as default.
    : :
    :
    : sscanf() approach is a much neater and shorter solution to verify if
    : a given string is double or not compared to the one that converts the
    : string to float, prints it into a string, and then compares the two
    : strings.
    :

    That's not what I'm arguing about. What I'm saying is that you use sscanf() where atof would have given exactly the same result, with less overhead.

    And as I said, that code with sprintf() has more functionality than the one with sscanf(). It will detect if there is any junk letters at the end, which the sscanf() code will not.

    Summary:
    To parse a double from a string with no care taken of junk letters, use atof() or strtof().
    If you want to detect junk letters, you might use the sprintf() version I wrote, sence it will do a check if the string is identical after the conversion.

    Now if you really want high performance you would write a custom function that checks for digits and other acceptable float characters in the string. All printf/scanf functions are incredible slow.
    But if you want high performance you should avoid float numbers in the first place, and instead try to find a solution with plain integers, which is often possible.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories