Link to home
Start Free TrialLog in
Avatar of prain
prainFlag for United States of America

asked on

boost::lexical_cast does not convert floating pt. numbers correct

I have written a small template to convert a given number (in a string object) to any primitive type I want. The following code works good for an int. But the float types does not work and th eprecision is lost.

#include <string>
#include <boost/lexical_cast.hpp>
template<class T>
class StringUtilities
{
    public:
    static T toType(const std::string inString)
    {  
        T value = 0;
        try
        {
            value = boost::lexical_cast<T>(inString);
        }
        catch (std::exception & e)
        {
            std::cout << "Error while parsing" << std::endl;
         }

        return value;
    }
    };

Now when I use the above code...
main()
{
   td::string aStringInt1 = "45";
   int iVal = StringUtilities<int>::toType(aStringInt1);   //This is OK

    std::string aStringInt2 = "23451.3";
    std::cout << StringUtilities<float>::toType(aStringInt2) << std::endl; //This is OK

   //THIS IS NOT OK. IT DROPS THE SECOND DECIMAL PLACE
    std::string aStringInt3 = "23451.35";
    std::cout << StringUtilities<float>::toType(aStringInt3) << std::endl; //This is NOT OK

   //THE SAME WITH ANY LARGER floating pt number and some come out as EXPONENTS
   //after dropping the precisoin. For example the following example procudes the
   //value 2.34513e+07
    std::string aStringInt4 = "23451344.35333";
    std::cout << StringUtilities<float>::toType(aStringInt4) << std::endl; //This is NOT OK
}

How can I produce the same result? What I mean is if I input "23451344.35333" I want the
output as 23451344.35333

Saving the precision is atmost priority for me in my application. Any suggestions?





}
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of prain

ASKER

I just did that. It does nto seem to give me the correct answer.

static T toType(const std::string inString)
    {  
        T value = 0;
        try
        {
            //value = boost::lexical_cast<T>(inString);
            std::stringstream ss(inString);
            ss >> value;
        }
        catch (std::exception & e)
        {
            std::cout << "Error while parsing" << std::endl;
         }

        return value;
    }
This still returned in Exponent  format.
Hm, if thge output format is the primary issue, 'iomanip' will help, e.g.

#include <iomanip>

std::cout << std::setiosflags(std::ios_base::showpoint) << StringUtilities<float>::toType(aStringInt3) << std::endl; 

Open in new window

Avatar of prain

ASKER

OK Thanks. Yes I did not realize that. The value is saved in the word corectly. It's a matter of outputting.
Avatar of prain

ASKER

Actually boost::lexical_cast also workd good. Like I have said in my comment, it's a matter of outputting.