1. 字符串转数字

如将“32”转为32,将“3.1415”转为3.1415,将“567283”转为567283。使用:

//Convert string to integer, more @http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/
int atoi ( const char * str );
//Convert string to double, more @http://www.cplusplus.com/reference/clibrary/cstdlib/atof/
double atof ( const char * str );
// Convert string to long integer,more @http://www.cplusplus.com/reference/clibrary/cstdlib/atol/
long int atol ( const char * str );
//Read formatted data from string, more @http://www.cplusplus.com/reference/clibrary/cstdio/sscanf/
int sscanf ( const char * str, const char * format, ...);

Example1:

/* atoi,atof,atol example */
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int a;float b;long c;
a=atoi("32");
b=atof("3.1415");
c=atol("567283");
printf ("%d,%f,%d",a,b,c);
return 0;
} Output:
32,3.141500,567283

Example2:

/* sscanf example */
#include <stdio.h>
int main ()
{
int a;float b;long c;
sscanf ("32,3.1415,567283","%d,%f,%d",&a,&b,&c);
printf ("%d,%f,%d",a,b,c);
return 0;
} Output:
32,3.141500,567283

2. 数字字符串

如将32转为“32”,将3.1415转为“3.1415”,将567283转为“567283”。使用:

//Write formatted data to string, more @http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/
int sprintf ( char * str, const char * format, ... );
//Convert integer to string (non-standard function), more @http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/
//This function is not defined in ANSI-C and is not part of C++, but is supported by some compilers.
//A standard-compliant alternative for some cases may be sprintf.
char * itoa ( int value, char * str, int base );

Example3:

Example3

/* sprintf example */
#include <stdio.h>
int main ()
{
char a[10],b[10],c[10];
sprintf(a, "%d", 32);
sprintf(b, "%f", 3.1415);
sprintf(c, "%d", 567283);
printf("%s,%s,%s",a,b,c);
return 0;
} Output:
32,3.141500,567283

3. 使用stringstream进行字符串与数字的转换

这里给出一个例子,更多 @http://www.cppblog.com/Sandywin/archive/2008/08/27/27984.htmlhttp://blog.csdn.net/touzani/article/details/1623850

Example4:

Example4

// using stringstream constructors.
#include <iostream>
#include <sstream>
using namespace std;
int main ()
{
int a;float b;long c;
char d[10],e[10],f[10]; /* string to number */
stringstream ss;
ss << "32";
ss >> a;
ss.clear(); ss << "3.1415";
ss >> b;
ss.clear(); ss << "567283";
ss >> c;
ss.clear(); cout<<a<<","<<b<<","<<c<<endl; /* number to string */
ss << a;
ss >> d;
ss.clear(); ss << b;
ss >> e;
ss.clear(); ss << c;
ss >> f;
ss.clear(); cout<<d<<","<<e<<","<<f<<endl; return 0;
} Output:
32,3.141500,567283
32,3.141500,567283

小结:C++标准库中的<sstream>提供了比ANSI C的<stdio.h>更高级的一些功能,即单纯性、类型安全和可扩展性,可以使用这些库来实现安全和自动的类型转换。言下之意就是,使用stringstream进行字符串与数字的转换是更好的选择。

C++字符串转数字,数字转字符串的更多相关文章

  1. 写出将字符串中的数字转换为整型的方法,如:“as31d2v”->312,并写出相应的单元测试,正则去掉非数值、小数点及正负号外的字符串

    写出将字符串中的数字转换为整型的方法,如:"as31d2v"->312,并写出相应的单元测试,输入超过int范围时提示不合法输入. public struct Convert ...

  2. Python 判断字符串是否为数字

    转载: http://www.runoob.com/python3/python3-check-is-number.html 以下实例通过创建自定义函数 is_number() 方法来判断字符串是否为 ...

  3. Oracle 把秒转成时分秒格式(hh24:mm:ss);检测字符串是否是数字;字符串转换为数字

    不说废话,贴代码: CREATE OR REPLACE FUNCTION to_time(sec IN NUMBER) RETURN VARCHAR2 IS /*把秒转成时分秒格式 auth lzpo ...

  4. C#判断字符串是否是数字

    /// <summary> /// 判断字符串是否是数字 /// </summary> public static bool IsNumber(string s) { if ( ...

  5. Java--正则表达式-简单的在字符串中找数字

    import org.junit.Test; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ...

  6. SQL Server 2008 R2——创建函数 筛选出字符串中的数字 筛选出字符串中的非数字

    =================================版权声明================================= 版权声明:本文为博主原创文章 未经许可不得转载  请通过右 ...

  7. Asp.net,C# 纯数字加密解密字符串

    也就是说加密后的数据不再是:N8lAaHMFtSAQgaf3+RUFng== 希望encryptedString是"1203877893704809384098328409234923840 ...

  8. 将字符串转化为数字(Convert和Parse的用法)

    字符串必须是数字,不要超过转换成目标数字类型的范围.超过的话系统也会报错(溢出). static void Main(string[] args) { string s; int i; Console ...

  9. 验证一个字符串是否由数字组成(Java)

    public class StringDemo{ public static void main(String args[]){ String str ="12343264sd6223&qu ...

  10. Excel中如何提取字符串中的数字

    取字符串中的数字,假如数据在A列,提取公式为 =LOOKUP(9^9,--MID(A1,MIN(FIND({1,2,3,4,5,6,7,8,9,0},A1&5^19)),ROW($1:$99) ...

随机推荐

  1. 【Linux】VirtualBox虚拟网络配置

    Host OS : Windows 10 Guest OS : CentOS 6.8 VirtualBox:5.1.18 网络连接方式: NAT 1.CentOS中使用DHCP [root@gouka ...

  2. A Fast and Easy to Use AES Library

    http://www.codeproject.com/Articles/57478/A-Fast-and-Easy-to-Use-AES-Library Introduction EfAesLib i ...

  3. LeetCode(108) Convert Sorted Array to Binary Search Tree

    题目 Given an array where elements are sorted in ascending order, convert it to a height balanced BST. ...

  4. redis和memcached的优缺点及区别

    1. 使用redis有哪些好处? (1) 速度快,因为数据存在内存中,类似于HashMap,HashMap的优势就是查找和操作的时间复杂度都是O(1) (2) 支持丰富数据类型,支持string,li ...

  5. Experiments done

    喷重金属 换重金属溶液 荧光光合 备注 ASD 备注 高光谱 备注 泡EDTA 备注 电镜 备注 2018.12.19(day1) 2018.12.19(day1) 2018.12.18晚(day0) ...

  6. Knockout v3.4.0 中文版教程-10-绑定-控制文本内容和外观-visible绑定

    4.绑定 1. 控制文本内容和外观 1. visible绑定 目的 visible绑定可以根据你传入绑定的值控制关联的DOM元素显示或隐藏. 例子 <div data-bind="vi ...

  7. ci $this->load->database()

    http://pengbotao.cn/codeigniter-database.html

  8. appium+python自动化-xpath定位

    基本属性定位 以淘宝app为例,定位左上角扫一扫按钮 1.可以通过text文本定位到 //*[@text='text文本属性'] # 定位text driver.find_element_by_xpa ...

  9. Codeforces Round #412 Div. 2 补题 D. Dynamic Problem Scoring

    D. Dynamic Problem Scoring time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  10. 洛谷P2351 [SDOi2012]吊灯 【数学】

    题目 Alice家里有一盏很大的吊灯.所谓吊灯,就是由很多个灯泡组成.只有一个灯泡是挂在天花板上的,剩下的灯泡都是挂在其他的灯泡上的.也就是说,整个吊灯实际上类似于[b]一棵树[/b].其中编号为 1 ...