Solved

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

Posted on 2013-01-16
5
587 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

733 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