Thread: can atoi convert string to float number?

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    can atoi convert string to float number?

    is there any function like atoi() but convert the string to a float number??

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    atof()

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    thnak you so much...but....

    but what if my string = "2219.5851"??

    as atof(string) = 2219.59.......

    how can i get the exact float number??

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    For this you may want to use one of the scanf functions. Such as sscanf(string, "%.3g", &my_float).

    [edit]
    Oops, I didn't read that correctly.
    The return is probably correct, floats are perfect, and it is possible that printf is the one doing the rounding.
    [/edit]
    Last edited by master5001; 03-14-2003 at 03:12 AM.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    ....cout too

    mmmmm i use cout to print the output....
    but the number also rounded.....

    and see the examples in MSDN........seems the result are rounded too......

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    strtod()

    Check the FAQ for using strtol() which is the long int version of this function, you can base your code on those examples.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    28

    Re: ....cout too

    Originally posted by Jasonymk
    mmmmm i use cout to print the output....
    but the number also rounded.....

    and see the examples in MSDN........seems the result are rounded too......
    Code:
    #include <iostream> // std::cout, std::endl
    #include <iomanip>  // std::setprecision
    
    int main()
    {
        double d = 1234.56789;
        std::cout << d << std::endl; // output: 1234.57;
        std::cout << std::setprecision(10) << d << std::endl; // output 1234.56789
        return 0;
    }

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. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM