這篇文章主要介紹“如何使用C++中atof函數”,在日常操作中,相信很多人在如何使用C++中atof函數問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”如何使用C++中atof函數”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
在 stdlib.h
中 atof
函數,可用于將 char
字符串轉為 float / double
浮點數類型,
語法如下:
/* *描述:將一個char類型轉為浮點數double * *參數: * [in] str:字符串類型 * *返回值:返回char類型對應的浮點數double */ double atof ( const char * str );
#include "stdafx.h" #include <stdio.h> #include "windows.h" #pragma warning(disable: 4996) int _tmain(int argc, _TCHAR* argv[]) { printf("atof函數計算結果 %f \n", atof("13456")); printf("atof函數計算結果 %f \n", atof("0")); printf("atof函數計算結果 %f \n", atof("789")); printf("atof函數計算結果 %f \n", atof("123.123")); printf("atof函數計算結果 %f \n", atof("-9")); system("pause"); return 0; }
輸出:
atof函數計算結果 13456.000000
atof函數計算結果 0.000000
atof函數計算結果 789.000000
atof函數計算結果 123.123000
atof函數計算結果 -9.000000
請按任意鍵繼續. . .
注意占位符的使用:
浮點是使用 %f
整數是使用 %d
char字符是使用 %c
char字符串是使用 %s
到此,關于“如何使用C++中atof函數”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。