Thread: float from a string

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    66

    float from a string

    What is wrong with this code:

    #include <stdio.h>
    #include <math.h>

    void main()
    {
    char str[] = "123456789" ;
    float f;

    f = ( atof( str ) / 100 );
    printf(" %f7.2", f);
    }

    It doesn't give me the desired result of 1234567.89

    Please could you explain why this is and how I can achieve it.
    T

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    one make f a double.

    double f;

    two,

    your format specifier is wrong

    printf(" %f7.2", f); should be

    printf(" %7.2f", f);
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    66
    If I use:
    double f;

    instead of
    float f;

    how can I convert the string instead of atof();

    T
    T

  4. #4
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    >how can I convert the string instead of atof();

    i don't get you meaning atof() will work with doubles too, since all your after is the return?
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  5. #5
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    You need to cast the result of atof to a float or use a double variable. Your compiler should give you a warning if you don't about converting double to float. void main is not right, it's possible that the runtime startup code might not be able to call main properly if it isn't defined with a very specific tag, that tag is

    int main()
    or
    int main( int argc, char *argv[] )

    You can also use just

    main()
    or
    main( int argc, char *argv[] )

    because if you don't give any return value at all C will assume you meant to use int. It's a neat trick if that kind of thing appeals to you.

    You also don't want to use atof because if the string you pass to it is too big for a double then anything can happen. And atof is declared in stdlib.h, not math.h. It may be in math.h on your compiler, but that isn't a standard practice.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	char str[] = "123456789";
    	float f;
    	
    	f = (float)( strtod( str, NULL ) / 100 );
    	printf("%7.2f\n", f);
    	return 0;
    }
    Bebop
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM