Solved

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

Posted on 2013-01-16
5
576 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

705 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