Thread: atof() truncating data?

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    43

    Question atof() truncating data?

    For some reason, the value of f is truncated. I get 26546.3 as output. I've tried changing the precision to no avail. Is there something I'm missing?
    Code:
    string temp = "26546.298";
    double result = 0.0;
    result = atof(temp.c_str());
    cout << result << endl;

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    the default floating-point precision for the cout stream is 6, meaning you did something wrong when setting the precision. what was the statement you had for setting precision? its straightforward to use so if you cant get it working you could just look up an example of 'setprecision'

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    43
    Quote Originally Posted by nadroj View Post
    the default floating-point precision for the cout stream is 6, meaning you did something wrong when setting the precision. what was the statement you had for setting precision? its straightforward to use so if you cant get it working you could just look up an example of 'setprecision'
    I tried changing the precision because it wasn't working with defaults. Regardless, I tried both cout.precision() and setprecision().

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    This code:
    Code:
    #include <string>
    #include <iostream>
    #include <cstdlib>
    
    int main() {
        std::string temp = "26546.298";
        double result = 0.0;
        result = atof(temp.c_str());
        std::cout.precision(8);
        std::cout << result << std::endl;
        return 0;
    }
    produces this output (on gcc for mac):
    Code:
    26546.298

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. Replies: 1
    Last Post: 07-31-2002, 11:35 AM