CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

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

1. Senior Member
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. Senior Member
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. Re: [RESOLVED] how convert char* to double?

Originally Posted by Cambalinho
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)?

4. 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;
}```

5. Senior Member
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
•