dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: [RESOLVED] how convert char* to double?

  1. #1
    Join Date
    Apr 2009
    Posts
    1,250

    [RESOLVED] how convert char* to double?

    see these char*: "3.1415926"
    i used the atof() for convert it, but i only get "3.14159"...
    so how can i convert with more precision?
    i even tryied with stold() and stod():
    Code:
    double ToDouble()    {
            if(IsNumeric()==true)
            {
                return stold(chrString);
            }
            else
                return 0;
        }

  2. #2
    Join Date
    Apr 2009
    Posts
    1,250

    Re: how convert char* to double?

    i didn't knew, we must change the precision on cout:
    Code:
    strTest="3.1415926";    double intValue=strTest.ToDouble();
        cout <<"String: "<< setprecision(10)  << intValue<<"\n";

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,591

    Re: [RESOLVED] how convert char* to double?

    Quote Originally Posted by Cambalinho View Post
    see these char*: "3.1415926"
    i used the atof() for convert it, but i only get "3.14159"...
    so how can i convert with more precision?
    And how did you test/check that the precision is only "3.14159" (only 5 digits after the decimal point)?
    Victor Nijegorodov

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,978

    Re: [RESOLVED] how convert char* to double?

    Code:
    double ToDouble()    {
            if(IsNumeric()==true)
            {
                return stold(chrString);
            }
            else
                return 0;
        }
    You are returning a double, but stold() returns a long double. For just a double use stod(). Note that it's not particularly good practice to return an error condition which is also a possible valid value. If chrString contains say "0.0" which is valid then 0.0 is returned which is the same as if "q.2" is tried to be converted.

    Also consider

    Code:
    double ToDouble() {
        return IsNumeric() ? stod(chrString) : 0.0;
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.5)

  5. #5
    Join Date
    Apr 2009
    Posts
    1,250

    Re: [RESOLVED] how convert char* to double?

    (i'm sorry.. i was out of network)
    VictorN i was print it using cout.
    2kaud: using your code, i continue just print:
    Code:
    class String{
    private:
        char *chrString=NULL;
    public:
        String(const char *chrValue)
        {
            if(chrString!=NULL)
                delete [] chrString;
            chrString=new char[strlen(chrValue)+1];
            strcpy(chrString,chrValue);
        }
    
    
        String(const String &chrValue)
        {
            if(chrString!=NULL)
                delete [] chrString;
            chrString=new char[strlen(chrValue.chrString)+1];
            strcpy(chrString,chrValue.chrString);
        }
    
    
        String& operator=(const char *chrValue)
        {
            if(chrString!=NULL)
                delete [] chrString;
            chrString=new char[strlen(chrValue)+1];
            strcpy(chrString,chrValue);
            return *this;
        }
    
    
        String& operator+(const char *chrValue)
        {
            strcat(chrString,chrValue);
            return *this;
        }
    
    
        String& operator+=(const char *chrValue)
        {
            strcat(chrString,chrValue);
            return *this;
        }
    
    
        operator const char*()
        {
            return chrString;
        }
    
    
        operator string()
        {
            string strValue(chrString);
            return strValue;
        }
    
    
        char &operator[](int index)
        {
            if((unsigned int)index>=strlen(chrString))
                index=strlen(chrString)-1;
            else if((unsigned int)index<0)
                index=0;
    
    
            return chrString[index];
        }
    
    
        void operator ()(const char *chrValue)
        {
            cout <<chrValue;
        }
    
    
        bool IsNumeric()
        {
            for(unsigned int i=0; i<strlen(chrString); i++)
            {
                if(chrString[i]=='.')
                    continue;
                if(isdigit(chrString[i])==false)
                    return false;
            }
            return true;
        }
    
    
    
    
        double ToDouble()
        {
            if(IsNumeric()==true)
            {
                return stold(chrString);
            }
            else
                return 0.0;
        }
    
    
        float ToFloat()
        {
            if(IsNumeric()==true)
                return atof(chrString);
            else
                return 0.0;
        }
    
    
        int ToInt()
        {
            if(IsNumeric()==true)
                return atoi(chrString);
            else
                return 0;
        }
    
    
        long long ToLongLong()
        {
            if(IsNumeric()==true)
                return atoll(chrString);
            else
                return 0;
        }
    
    
        long ToLong()
        {
            if(IsNumeric()==true)
                return atol(chrString);
            else
                return 0;
        }
    
    
        ~String()
        {
             if(chrString!=NULL)
                delete [] chrString;
        }
    };
    //testing:
    int main()
    {
        String strHello="3.1415926";
        double dbl=strHello.ToDouble();
        std::cout << dbl;
        return 0;
    }
    output:
    3.14159
    now lets change the precision:
    Code:
    int main(){
        String strHello="3.1415926";
        double dbl=strHello.ToDouble();
        std::cout << setprecision(8) << dbl;
        return 0;
    }
    output:
    3.1415926
    these precision is limited for 7 numbers after dot.
    thanks to all

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)