C语言中将数字转换为字符串的方法
C语言提供了几个标准库函数,可以将任意类型(整型、长整型、浮点型等)的数字转换为字符串。以下是用itoa()函数将整数转换为字符串的一个例子:
# include <stdio. h>
# include <stdlib. h>
void main (void);
void main (void)
{
int num = 100;
char str[25];
itoa(num, str, 10);
printf("The number 'num' is %d and the string 'str' is %s. \n" ,
num, str);
}
itoa()函数有3个参数:第一个参数是要转换的数字,第二个参数是要写入转换结果的目标字符串,第三个参数是转移数字时所用的基数。在上例中,转换基数为10。
下列函数可以将整数转换为字符串:
----------------------------------------------------------
函数名 作 用
----------------------------------------------------------
itoa() 将整型值转换为字符串
itoa() 将长整型值转换为字符串
ultoa() 将无符号长整型值转换为字符串
----------------------------------------------------------
请注意,上述函数与ANSI标准是不兼容的。能将整数转换为字符串而且与ANSI标准兼容的方法是使用sprintf()函数,请看下例:
#include<stdio.h>
# include <stdlib. h>
void main (void);
void main (void)
{
int num = 100;
char str[25];
sprintf(str, " %d" , num);
printf ("The number 'num' is %d and the string 'str' is %s. \n" ,
num, str);
}
在将浮点型数字转换为字符串时,需要使用另外一组函数。以下是用fcvt()函数将浮点型值转换为字符串的一个例子:
# include <stdio. h>
# include <stdlib. h>
void main (void);
void main (void)
{
double num = 12345.678;
char * sir;
int dec_pl, sign, ndigits = 3; /* Keep 3 digits of precision. * /
str = fcvt(num, ndigits, &dec-pl, &sign); /* Convert the float
to a string. * /
printf("Original number; %f\n" , num) ; /* Print the original
floating-point
value. * /
printf ("Converted string; %s\n",str); /* Print the converted
string's value. * /
printf ("Decimal place: %d\n" , dec-pi) ; /* Print the location of
the decimal point. * /
printf ("Sign: %d\n" , sign) ; /* Print the sign.
0 = positive,
1 = negative. * /
}
fcvt()函数和itoa()函数有数大的差别。fcvt()函数有4个参数:第一个参数是要转换的浮点型值;第二个参数是转换结果中十进制小数点右侧的位数;第三个参数是指向一个整数的指针,该整数用来返回转换结果中十进制小数点的位置;第四个参数也是指向一个整数的指针,该整数用来返回转换结果的符号(0对应于正值,1对应于负值)。
需要注意的是,fcvt()函数的转换结果中并不真正包含十进制小数点,为此,fcvt()函数返回在转换结果中十进制小数点应该占据的位置。在上例中,整型变量dec_pl的结果值为5,因为在转换结果中十进制小数点应该位于第5位后面。如果你要求转换结果中包含十进制小数点,你可以使用gcvt()函数(见下表)。
下列函数可以将浮点型值转换为字符串:
-------------------------------------------------------------------------
函数名 作 用
-------------------------------------------------------------------------
ecvt() 将双精度浮点型值转换为字符串,转换结果中不包含十进制小数点
fcvt() 以指定位数为转换精度,余同ecvt()
gcvt() 将双精度浮点型值转换为字符串,转换结果中包含十进制小数点
C语言中将数字转换为字符串的方法的更多相关文章
- Postgresql/Greenplum中将数字转换为字符串TO_CHAR函数前面会多出一个空格
-- 问题1..Postgresql中将数字转换为字符串前面多出一个空格. SELECT TO_CHAR(, '); -- 解决1.使用如下,参数二前面加上fm就可以去掉空格了,如下: SELECT ...
- Javascript中将数字转换为中文的方法
//js实现将数字1234转化为汉字字符串(一千二百三十四)(或大写汉字壹仟贰佰叁拾肆): /*阿拉伯数字转中文数字 中文数字的特点: 每个计数数字都跟着一个权位,权位有:十.百.千.万.亿. 以“万 ...
- C语言把数字转换为字符串的函数
博主原文 C语言itoa()函数和atoi()函数详解(整数转字符C实现) C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串. 1.int/float to st ...
- Linq to SQL 中将数字转换为字符串
使用LINQ to Entities中的SqlFunctions调用数据库中的函数 添加引用System.Data.Entity 引用命名空间 using System.Data.Objects.Sq ...
- django中将model转换为dict的方法
django中将model转换为dict的方法 from django.forms.models import model_to_dict from user.model import userpro ...
- VC++和C语言中常见数据类型转换为字符串的方法
1.短整型(int) itoa(i,temp,10);///将i转换为字符串放入temp中,最后一个数字表示十进制 itoa(i,temp,2); ///按二进制方式转换 2.长整型(long) lt ...
- python 数字以及字符串(方法总结,有的可能理解错误)
数字类型(int): 在python 2中,数字类型可以分为整形,长整形,浮点型,以及复数.在python3中都是整形和长整形都称之为整形,且python3中没有限制. 1.int方法使用,用于转换字 ...
- 怎样避免C#中将小数转换为字符串时出现科学记数法
在C#中如果float.double.decimal类型的值,小数点后的0太多时,C#会用科学记数法来表示小数的值. 例如下面的double类型0.00009,如果我们直接将其用ToString()方 ...
- C语言把整数转换为字符串
目录 1.把整数/长整数格式化输出到字符串 2.注意事项 3.版权声明 各位可能在网上看到用以下函数可以将整数转换为字符串: itoa(); //将整型值转换为字符串 ultoa(); // 将无符号 ...
随机推荐
- [iOS微博项目 - 3.2] - 发送微博
github: https://github.com/hellovoidworld/HVWWeibo A.使用微博API发送微博 1.需求 学习发送微博API 发送文字微博 发送带有图片的微博 ...
- C# JackLib系列之如何获取地球上两经纬度坐标点间的距离
获取地球上两经纬度坐标点间的距离,利用[大圆距离公式] A diagram illustrating great-circle distance (drawn in red) between tw ...
- F5 负载均衡 相关资源
F5负载均衡之检查命令的说明http://net.zdnet.com.cn/network_security_zone/2010/0505/1730942.shtml F5培训http://wenku ...
- erlang pool模块。
出自: http://blog.sina.com.cn/s/blog_96b8a154010168ti.html
- svn 冲突解决
svn: E155015: Aborting commit: '$path + $file' remains in conflict 解决步骤 1.svn resolved 'file'执行后结果Re ...
- 笔记本键盘上没有break键的解决方案
django在Windows上调试需要用ctrl+break终止服务器……笔记本键盘上没有break好尴尬…… 在百度搜了很多都没有找到,最后终于在谷歌上找到了英文版的解决方案. starting o ...
- 对于C语言中数组名是指针的理解
我们都知道,c语言中数组名是一个指针,比如下面这段代码 #include<iostream>using namespace std;int main(){ int a[4]={1,2,3, ...
- ARM Cortex-M instructions
ARM Cortex-M instruction sets ARMCortex-M Thumb Thumb-2 Hardwaremultiply Hardwaredivide Saturatedmat ...
- 防火墙没关导致 ORA-12541: TNS: 无监听程序
电脑用着用着突然Oracle就报出下面的错误,按照网上的办法搞了几个小时都没有搞好. Oracle重装了好几次也没用,实在没办法又花了个多小时装了个虚机,结果也是同样的错误. 于是恍然大悟,可能是物理 ...
- Asp.Net BulletedList
BulletedList使用及详解 BulletedList是一个让你轻松在页面上显示项目符号和编号格式(Bulledted List)的控件.对于ASP.NET 1.x里要动态显示Bulledted ...