Boost logo

Boost Users :

Subject: [Boost-users] [lexical_cast] float-to-string rounding error (not found with cout)
From: Grant Birchmeier (gbirchmeier_at_[hidden])
Date: 2009-04-30 12:38:14


I'm seeing unexpected behavior when using lexical_cast<string>.

This code:
    const float f = 00123.12300;
    cout << f << endl;
    cout << boost::lexical_cast<std::string>(f) << endl;
gives the result:
    123.123
    123.123001

Two questions:

(1) Is there a way to eliminate this trailing '1' with lexical_cast
(presumably some wierd rounding error)?

(2) When casting float-to-string, given this behavior, why should I
use lexical_cast instead of, say, stringstream?

Thanks
-Grant


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net