Solved

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

Posted on 2013-01-16
5
580 Views
Last Modified: 2013-01-16
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?





}
0
Comment
Question by:prain
  • 3
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 38783302
>>Any suggestions?

Like in your last Q - use a stringstream for the conversion, e.g.

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

        if (ss.fail()) 
        {
           // handle error
        }

        return value;
    }
    };

Open in new window

0
 

Author Comment

by:prain
ID: 38783538
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.
0
 
LVL 86

Expert Comment

by:jkr
ID: 38783820
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

0
 

Author Comment

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

Author Closing Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now