Conversion between double and string in C + +, char *

Source: Internet
Author: User
Tags deprecated sprintf

/** main.cpp * * Created on:apr 7 * author:lizhen*/#include<iostream>//#include "MySqrt.h"#include <math.h> #include <vector> #include <typeinfo>#include<exception>#include<stdexcept>#include<string.h>#include<sstream>#include<stdio.h>using namespacestd;classbase{ Public: Base () {cout<<"Create the base"<<Endl; }    Virtual~Base () {cout<<"destroy the base"<<Endl; }};classDerived: Publicbase{ Public: Derived () {cout<<"Derived is created"<<Endl; }    Virtual~Derived () {cout<<"Derived is destroying"<<Endl; }};//Double-->stringstringDoubleconvertostring (Doubled)    {Ostringstream OS; if(OS << D)returnOs.str (); return "Invalid conversion";}//string-->doubleDoubleStringconvertodouble (stringstr)    {istringstream iss (str); Doublex; if(ISS >> x)returnx; return 0.0;}//c-function double-->stringstringCfunctiondtos (Doubled) {    Charstr[ -]; sprintf (str,"%.3LF", D); returnstr;}//c-function string->doubleDoubleCfunctionstod (stringstr) {    DoubleDD; SSCANF (Str.c_str (),"%LF",&DD); returnDD;}intMain () {//string-->char*    stringStr"string"); Char*p = const_cast<Char*>(Str.c_str ()); cout<<"string->char*"<<p<<Endl; //char*-->string    Char*ch = const_cast<Char*> ("Char");//warning:deprecated Conversion from string constant to ' char* ' [-wwrite-strings]|    stringchstr (CH); cout<<"char *-->string"<<chstr<<Endl; //double&float--->string    DoubleDD =3.14; stringDdstr =doubleconvertostring (DD); cout<<ddstr<<Endl; //String--->double&float    stringSTRP ="3.5555555555"; DoubleStrdd =stringconvertodouble (STRP); cout<<strdd<<Endl; cout<<atof (Strp.c_str ()) <<Endl; //c-function double->string    stringSS = Cfunctiondtos (3.146789); cout<<"SS"<<ss<<Endl; //c-function string->string    DoubleCDD = Cfunctionstod ("3.14259"); cout<<cdd<<Endl;}

Run results

1 string-char*string2Char *--Stringchar3 3.14 4 3.55556 5 3.55556 6 SS3. 147 7 3.14259

===========================================================

string-->char*

// string-->char*    string str ("string");     char *p = const_cast<char *>(Str.c_str ());    cout<<"string->char*"<<p<<endl;

Char*-->string

// char*-->string    char *ch = const_cast<char *> ("char"); // warning:deprecated Conversion from string constant to ' char* ' [-wwrite-strings]|    string chstr (CH);    cout<<"char *-->string"<<chstr<<endl;

===

Double/float-->string

String-->double/float

Using methods from the Sstream header file in C + +

= = outputs a double to a string using the Ostringstream output stream object

Here's how:

// Double-->string string doubleconvertostring (double  d) {    ostringstream os;     if return os.str ();     return " Invalid conversion " ;}

= = Use the Istringstream input stream object to place the contents of the string in a double variable

Methods are as follows

// string-->double double stringconvertodouble (string  str) {    istringstream iss (str);     Double   x;     if return x;     return 0.0 ;}

Using the methods in the Stdio.h header file in standard C

= = Use the sprintf (str, "%.3LF", dd) method to output the characters in a double variable to the string str

Here's how:

// c-function double-->string string cfunctiondtos (double  d) {    char str[];    sprintf (str,"%.3lf", D);     return str;}

= = Use the sscanf (str, "%d", &dd) method to place the contents of the string str in the double variable DD

Here's how:

// c-function string->double double cfunctionstod (string  str) {    double  DD;    SSCANF (Str.c_str (),"%lf",&dd);     return DD;}

Other methods

char *itoa (int value, char* string, int radix); int---->string
It is also possible to convert a number to a string, but itoa () is a platform-dependent (not standard) function, so it is not recommended here.

You can also use Atoi (), Atol (), Atof (). String can be--->int/double/float

Reference Documentation:

Http://www.cnblogs.com/luxiaoxun/archive/2012/08/03/2621803.html

Conversion between double and string in C + +, char *

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.