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

    Ctrl+Alt+(F1-F6):切换虚拟终端 Ctrl+Alt:鼠标切换界面 $:普通用户登录后系统的提示符 #:root用户登录后系统的提示符 Linux命令 exit  用于退出目前的shell ...

  2. 译文 编写一个loader

    https://doc.webpack-china.org/contribute/writing-a-loader loader是一个导出了函数的node模块,当资源须要被这个loader所转换的时候 ...

  3. 关于加号传递到后端会变为空格的c#例子

    参考博客:http://blog.csdn.net/nsdnresponsibility/article/details/50965262 以前在一次传递参数的情况中遇到,特此记录一下. 之前传递的参 ...

  4. Python标准库之csv(1)

    Python标准库之csv(1) 1.Python处理csv文件之csv.writer() import csv def csv_write(path,data): with open(path,'w ...

  5. 利用MySQL数据库如何解决大数据量存储问题?

    提问:如何设计或优化千万级别的大表?此外无其他信息,个人觉得这个话题有点范,就只好简单说下该如何做,对于一个存储设计,必须考虑业务特点,收集的信息如下:1.数据的容量:1-3年内会大概多少条数据,每条 ...

  6. SPOJ DIVSUM - Divisor Summation

    DIVSUM - Divisor Summation #number-theory Given a natural number n (1 <= n <= 500000), please ...

  7. pytorch中Math operation操作:torch.ger()

    torch.ger(vec1, vec2, out=None) → Tensor Outer product of vec1 and vec2. If vec1 is a vector of size ...

  8. php 投票

    1.投票主界面(问题界面) <?php$db = new Mysqli("localhost","root","root"," ...

  9. NYOJ-78 圈水池,凸包裸模板!

    圈水池 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 刚做完HDU1392,就看到这个题,嗯,原代码改改就过了. 题意不多说了,会凸包的话很简单,不会也不难,这道题时限是4s ...

  10. EasyUI 动态生成列加分页

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat=&qu ...