Solved

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

832 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