1.double atof(const char *nptr);
把字符串轉(zhuǎn)換成浮點(diǎn)數(shù)值。
<math.h>或<stdlib.h>
nptr:帶轉(zhuǎn)換的字符串。
若無(wú)溢出,返回字符串的雙精度浮點(diǎn)數(shù)值。
int atoi(const char* nptr);
把字符串轉(zhuǎn)換成整型數(shù)。
<stdlib.h>
nptr:帶轉(zhuǎn)換的字符串。
成功轉(zhuǎn)換,返回字符串的整數(shù)值;若無(wú)法轉(zhuǎn)換返回0.
long atol(const char* nptr);
將字符串轉(zhuǎn)換成長(zhǎng)整形數(shù)值。
<stdlib.h>
nptr: 待轉(zhuǎn)換的字符串。
若無(wú)溢出,返回字符串的長(zhǎng)整形數(shù)值。
2.數(shù)字轉(zhuǎn)化為字符串
? ● itoa():將整型值轉(zhuǎn)換為字符串。
? ● ltoa():將長(zhǎng)整型值轉(zhuǎn)換為字符串。
? ● ultoa():將無(wú)符號(hào)長(zhǎng)整型值轉(zhuǎn)換為字符串。
? ● gcvt():將浮點(diǎn)型數(shù)轉(zhuǎn)換為字符串,取四舍五入。
? ● ecvt():將雙精度浮點(diǎn)型值轉(zhuǎn)換為字符串,轉(zhuǎn)換結(jié)果中不包含十進(jìn)制小數(shù)點(diǎn)。
? ● fcvt():指定位數(shù)為轉(zhuǎn)換精度,其余同ecvt()。
還可以使用sprintf系列函數(shù)把數(shù)字轉(zhuǎn)換成字符串,其比itoa()系列函數(shù)運(yùn)行速度慢。下列程序演示了如何使用itoa()函數(shù)和gcvt()函數(shù):
# include <stdio.h>
# include <stdlib.h>int main ()
{int num_int = 435;double num_double = 435.10f;char str_int[30];char str_double[30];itoa(num_int, str_int, 10); //把整數(shù)num_int轉(zhuǎn)成字符串str_int 參數(shù) 10表示按十進(jìn)制類型進(jìn)行轉(zhuǎn)換gcvt(num_double, 8, str_double); //把浮點(diǎn)數(shù)num_double轉(zhuǎn)成字符串str_double 參數(shù)8表示精確位數(shù)printf("str_int: %s\n", str_int);printf("str_double: %s\n", str_double);return 0;
}結(jié)果:
str_int: 435
str_double: 435.10001
如果不使用atoi或sprintf等庫(kù)函數(shù),可以通過(guò)把整數(shù)的各位上的數(shù)字加“0”轉(zhuǎn)換成char類型并存到字符數(shù)組中。但是要注意,需要采用字符串逆序的方法。如以下程序所示:
1 #include <iostream>
2 using namespace std;
3
4 void int2str(int n, char *str)
5 {
6 char buf[10] = "";
7 int i = 0;
8 int len = 0;
9 int temp = n < 0 ? -n: n; // temp為n的絕對(duì)值
10
11 if (str == NULL)
12 {
13 return;
14 }
15 while(temp)
16 {
17 buf[i++] = (temp % 10) + '0'; //把temp的每一位上的數(shù)存入buf
18 temp = temp / 10;
19 }
20
21 len = n < 0 ? ++i: i; //如果n是負(fù)數(shù),則多需要一位來(lái)存儲(chǔ)負(fù)號(hào)
22 str[i] = 0; //末尾是結(jié)束符0
23 while(1)
24 {
25 i--;
26 if (buf[len-i-1] ==0)
27 {
28 break;
29 }
30 str[i] = buf[len-i-1]; //把buf數(shù)組里的字符拷到字符串
31 }
32 if (i == 0 )
33 {
34 str[i] = '-'; //如果是負(fù)數(shù),添加一個(gè)負(fù)號(hào)
35 }
36 }
37
38 int main()
39 {
40 int nNum;
41 char p[10];
42
43 cout << "Please input an integer:";
44 cin >> nNum;
45 cout << "output: " ;
46 int2str(nNum, p); //整型轉(zhuǎn)換成字符串
47 cout<< p << endl;
48
49 return 0;
50 }結(jié)果:
程序中的int2str函數(shù)完成了int類型到字符串類型的轉(zhuǎn)換。在代碼第46行對(duì)int2str函數(shù)做了測(cè)試。程序的執(zhí)行結(jié)果如下所示:
Please input an integer: 1234
Output: 1234
如果輸入的是個(gè)負(fù)數(shù),程序執(zhí)行結(jié)果如下所示:
Please input an integer: -1234
Output: -1234
3.?字符串轉(zhuǎn)化為數(shù)字
? ● atof():將字符串轉(zhuǎn)換為雙精度浮點(diǎn)型值。
? ● atoi():將字符串轉(zhuǎn)換為整型值。
? ● atol():將字符串轉(zhuǎn)換為長(zhǎng)整型值。
? ● strtod():將字符串轉(zhuǎn)換為雙精度浮點(diǎn)型值,并報(bào)告不能被轉(zhuǎn)換的所有剩余數(shù)字。
? ● strtol():將字符串轉(zhuǎn)換為長(zhǎng)整值,并報(bào)告不能被轉(zhuǎn)換的所有剩余數(shù)字。
? ● strtoul():將字符串轉(zhuǎn)換為無(wú)符號(hào)長(zhǎng)整型值,并報(bào)告不能被轉(zhuǎn)換的所有剩余數(shù)字。
以下程序演示如何使用atoi ()函數(shù)和atof ()函數(shù):
1 # include <stdio.h>
2 # include <stdlib.h>
3
4 int main ()
5 {
6 int num_int;
7 double num_double;
8 char str_int[30] = "435"; //將要被轉(zhuǎn)換為整型的字符串
9 char str_double[30] = "436.55"; //將要被轉(zhuǎn)換為浮點(diǎn)型的字符串
10
11 num_int = atoi(str_int); //轉(zhuǎn)換為整型值
12 num_double = atof(str_double); //轉(zhuǎn)換為浮點(diǎn)型值
13
14 printf("num_int: %d\n", num_int);
15 printf("num_double: %lf\n", num_double);
16
17 return 0;
18 }結(jié)果:
num_int: 435
num_double: 436.550000
不使用庫(kù)函數(shù)將字符串轉(zhuǎn)換為數(shù)字:
1 #include <iostream>
2 using namespace std;
3
4 int str2int(const char *str)
5 {
6 int temp = 0;
7 const char *ptr = str; //ptr保存str字符串開頭
8
9 if (*str == '-' || *str == '+') //如果第一個(gè)字符是正負(fù)號(hào),
10 { //則移到下一個(gè)字符
11 str++;
12 }
13 while(*str != 0)
14 {
15 if ((*str < '0') || (*str > '9')) //如果當(dāng)前字符不是數(shù)字
16 { //則退出循環(huán)
17 break;
18 }
19 temp = temp * 10 + (*str - '0'); //如果當(dāng)前字符是數(shù)字則計(jì)算數(shù)值
20 str++; //移到下一個(gè)字符
21 }
22 if (*ptr == '-') //如果字符串是以“-”開頭,則轉(zhuǎn)換成其相反數(shù)
23 {
24 temp = -temp;
25 }
26
27 return temp;
28 }
29
30 int main()
31 {
32 int n = 0;
33 char p[10] = "";
34
35 cin.getline(p, 20); //從終端獲取一個(gè)字符串
36 n = str2int(p); //把字符串轉(zhuǎn)換成整型數(shù)
37
38 cout << n << endl;
39
40 return 0;
41 }程序執(zhí)行結(jié)果:
輸入:1234
輸出:1234
輸入:-1234
輸出:-1234
輸入:+1234
輸出:1234
參考:http://blog.sina.com.cn/s/blog_4c8a2a870100qgq7.html
總結(jié)
以上是生活随笔為你收集整理的atoi(),atof(),atol();iato(),fato(),lato()---字符串和数字互相转换的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。