首先推荐用用 C++ 的 stringstream。
    主要原因是操作简单。

    数字转字符串,int float 类型 同理

    1. to_string(num) 推荐
    1. #include <string>
    2. #include <sstream>
    3. int main(){
    4. double a = 123.32;
    5. string res;
    6. stringstream ss;
    7. ss << a;
    8. ss >> res;//或者 res = ss.str();
    9. return 0;
    10. }
    11. 字符串转数字,int float类型 同理
    12. int main(){
    13. string a = "123.32";
    14. double res;
    15. stringstream ss;
    16. ss << a;
    17. ss >> res;
    18. return 0;
    19. }
    20. 上面方法的优点就是使用简单方便,确定可能会相对别的方法来说慢一点,但是一般少量的数据可以忽略该因素。
    21. 别的方法
    22. 2、数字转字符串:
    23. 下面方法转自:[http://www.cnblogs.com/luxiaoxun/archive/2012/08/03/2621803.html](http://www.cnblogs.com/luxiaoxun/archive/2012/08/03/2621803.html)
    24. 使用sprintf()函数
    25. char str[10];
    26. int a=1234321;
    27. sprintf(str,”%d”,a);
    28. --------------------
    29. char str[10];
    30. double a=123.321;
    31. sprintf(str,”%.3lf”,a);
    32. -----------------------
    33. char str[10];
    34. int a=175;
    35. sprintf(str,”%x”,a);//10进制转换成16进制,如果输出大写的字母是sprintf(str,”%X”,a)
    36. ---------------------------------------------------------------
    37. char _itoa(int value, char_ string, int radix);
    38. 同样也可以将数字转字符串,不过itoa()这个函数是平台相关的(不是标准里的),故在这里不推荐使用这个函数。
    39. 3、字符串转数字:使用sscanf()函数
    40. char str[]=”1234321”;
    41. int a;
    42. sscanf(str,”%d”,&a);
    43. ………….
    44. char str[]=”123.321”;
    45. double a;
    46. sscanf(str,”%lf”,&a);
    47. ………….
    48. char str[]=”AF”;
    49. int a;
    50. sscanf(str,”%x”,&a); //16进制转换成10进制
    51. 另外也可以使用atoi(),atol(),atof().
    to_string(num)