这篇文章主要给大家分享的是C++中atof 函数的介绍,在 stdlib.h 中 atof 函数,可用于将 char 字符串转为 float / double 浮点数类型,想具体了解语法的小伙伴可以参考下面文章的内容,希望对大家有所帮助
一.atof 函数
在 stdlib.h
中 atof
函数,可用于将 char
字符串转为 float / double
浮点数类型,
语法如下:
/*
*描述:将一个char类型转为浮点数double
*
*参数:
* [in] str:字符串类型
*
*返回值:返回char类型对应的浮点数double
*/
double atof ( const char * str );
二.atof 函数函数实战
#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
函数的介绍的文章就介绍到这了,更多相关atof 函数内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!