| C++ Builder 串口控件 | | C++ Builder 编程技巧 | | C++ Builder 操作指南 | | C++ Builder 参考手册 | | 基础知识 | | cfloat 浮点数 | | cmath 数学函数 | | cstdlib 标准库函数 | | • atof, _ttof, _wtof | | • _atold, _ttold, _wtold | | • atoi, _ttoi, _wtoi | | • atol, _ttol, _wtol | | • _atoi64, _ttoi64, _wtoi64 | | • strtod, _tcstod, wcstod | | • strtof, _tcstof, wcstof | | • strtold, _strtold, _tcstold, wcstold, _wcstold | | • strtol, _tcstol, wcstol | | • strtoll, _tcstoll, wcstoll | | • strtoul, _tcstoul, wcstoul | | • strtoull, _tcstoull, wcstoull | | • itoa, _itot, _itow | | • ltoa, _ltoa, _ltot, _ltow | | • ultoa, _ultot, _ultow | | • _i64toa, _i64tot, _i64tow | | • _ui64toa, _ui64tot, _ui64tow | | • ecvt, _ecvt | | • fcvt, _fcvt | | • gcvt, _gcvt | | • abs, labs, llabs | | • div, ldiv, lldiv | | • div_t, ldiv_t, lldiv_t | | • _rotl, _crotl, _lrotl | | • _rotr, _crotr, _lrotr | | • rand, _lrand | | • srand | | • random | | • randomize | | • mbstowcs | | • mblen | | • mbtowc | | • mbtowc_cp | | • wcstombs | | • wctomb | | • wctomb_cp | | • swab, _swab | | • _fullpath, _tfullpath, _wfullpath | | • _makepath, _tmakepath, _wmakepath | | • _splitpath, _tsplitpath, _wsplitpath | | • getenv, _tgetenv, _wgetenv | | • putenv, _putenv, _tputenv, _wputenv | | • _searchenv, _tsearchenv, _wsearchenv | | • _searchstr, _tsearchstr, _wsearchstr | | • system, _tsystem, _wsystem | | • abort | | • atexit | | • atexit_t | | • exit, _exit | | • abort_handler_s | | • ignore_handler_s | | • constraint_handler_t | | • set_constraint_handler_s | | • _argc | | • _argv, _targv, _wargv; | | • _environ, _tenviron, _wenviron | | • EXIT_SUCCESS, EXIT_FAILURE | | • _MAX_PATH, _MAX_DRIVE, _MAX_DIR, _MAX_FNAME, _MAX_EXT | | • MB_CUR_MAX | | • RAND_MAX, LRAND_MAX | | System 字符串 | | System 日期和时间 | | System.Math.hpp 数学函数 | | 其他数据类型 | | VCL 基础类 | | VCL 应用程序 | | Pictures 图片 | | Graphics 绘图 | | Additional 控件 | | System 控件 | | A ~ Z 字母顺序排列的目录 | | 网友留言/技术支持 |
|
| strtod, _tcstod, wcstod - 字符串转 IEEE 双精度浮点数 (double), 可以返回指向转换出错字符的指针 |
strtod, _tcstod, wcstod:字符串转 IEEE 双精度浮点数 (double), 可以返回指向转换出错字符的指针
函数原型:
| double strtod(const char *s, char **endptr); |
| double wcstod(const wchar_t *s, wchar_t **endptr); |
头文件:
#include <cstdlib>
命名空间:
std
参数:
s:浮点数字符串,格式:"[空白字符][符号][数字][.][数字][e|E[符号]数字]",支持 "+INF"、"-INF"、"+NAN" 和 "-NAN"
endptr:是一个指针,这个指针指向一个字符指针,用来返回当解析结束时指向参数 s 当中的字符的指针。如果指向 s 字符串的结束符,说明没有错误,如果指向 s 当中的其他字符,说明解析到这个字符出错而停止解析。
返回值:
双精度浮点数,double 类型
如果参数 s 包含不可识别的字符,会终止于第一个不可识别的字符,返回前面可识别部分转换为浮点数的值 (如果第一个字符不可识别返回值为 0),不产生异常,不调用 _matherr。
如果溢出了,返回值为 ± HUGE_VAL,全局变量 errno 为 ERANGE,不产生异常,不调用 _matherr。
endptr:返回当解析结束时指向参数 s 当中的字符的指针。如果指向 s 字符串的结束符,说明没有错误,如果指向 s 当中的其他字符,说明解析到这个字符出错而停止解析。
| 字符串 |
转换结果 |
说明 |
| "1.2345" |
1.2345 |
普通的小数 |
| "1.23e45" |
1.23e+45 |
科学计数法:1.23×1045 |
| "1.23x45" |
1.23 |
包含不可识别的字符 'x',转换结果为 'x' 之前的内容:1.23 |
| "-1.23e-45" |
-1.23e-45 |
科学计数法:-1.23×10-45 |
| "1.23e4567" |
+INF |
1.23e4567 超出了 double 的范围,溢出,返回值为 +HUGE_VAL,即 +INF |
| "123" |
123 |
整数值 |
| "abc" |
0 |
不可识别的字符串,返回值为 0 |
| "+INF" |
+INF |
正无穷大:+INF |
| "-INF" |
-INF |
负无穷大:-INF |
| "+NAN" |
+NAN |
正不是数:+NAN |
| "-NAN" |
-NAN |
负不是数:-NAN |
注:
经过测试,bcc32 和 clang32 完全测试通过,而 clang64 在溢出的时候返回值不等于 HUGE_VAL,例如下面的例子的 _tcstod(_T("1.23e4567"));并且不识别 "+INF"、"-INF"、"+NAN" 和 "-NAN"。无论是 C++ Builder 自带的帮助,还是微软的 MSDN 里面,对溢出和无穷大、NAN 的返回值的描述都和本文的 “返回值” 描述一致,所以认为 C++ Builder 的 clang64 编译器对于这一组函数存在兼容性问题 (测试的 C++ Builder 版本为 10.1 Berlin 和 10.2 Tokyo)。
例1:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
UnicodeString s = Edit1->Text;
wchar_t *wp;
double x = wcstod(s.c_str(), &wp);
if(*wp)
ShowMessage(UnicodeString().sprintf(L"错误字符:'%c', 转换结果:x = %g", *wp, x));
else
ShowMessage(UnicodeString().sprintf(L"转换结果:x = %g", x));
} |

例2:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
UnicodeString s;
wchar_t *wp; _TCHAR *tp; char *cp;
Memo1->Lines->Add(s.sprintf(L"wcstod(L\"1.2345\", &wp) = %g", wcstod(L"1.2345", &wp) ));
Memo1->Lines->Add(s.sprintf(L"wcstod(L\"1.23e45\", &wp) = %g", wcstod(L"1.23e45", &wp) ));
Memo1->Lines->Add(s.sprintf(L"wcstod(L\"1.23x45\", &wp) = %g", wcstod(L"1.23x45", &wp) ));
Memo1->Lines->Add(s.sprintf(L"tcstod(_T(\"-1.23e-45\"), &tp) = %g",_tcstod(_T("-1.23e-45"), &tp) ));
Memo1->Lines->Add(s.sprintf(L"tcstod(_T(\"1.23e4567\"), &tp) = %g",_tcstod(_T("1.23e4567"), &tp) ));
Memo1->Lines->Add(s.sprintf(L"strtod(\"123\", &cp) = %g", strtod("123", &cp) ));
Memo1->Lines->Add(s.sprintf(L"strtod(\"abc\", &cp) = %g", strtod("abc", &cp) ));
Memo1->Lines->Add(s.sprintf(L"wcstod(L\"+INF\", &wp) = %g", wcstod(L"+INF", &wp) ));
Memo1->Lines->Add(s.sprintf(L"wcstod(L\"-INF\", &wp) = %g", wcstod(L"-INF", &wp) ));
Memo1->Lines->Add(s.sprintf(L"wcstod(L\"+NAN\", &wp) = %g", wcstod(L"+NAN", &wp) ));
Memo1->Lines->Add(s.sprintf(L"wcstod(L\"-NAN\", &wp) = %g", wcstod(L"-NAN", &wp) ));
Memo1->Lines->Add(s.sprintf(L"HUGE_VAL = %g" , HUGE_VAL ));
} |

兼容性:
| 函数 \ C++ Builder 编译器 |
bcc32 |
clang32 |
clang64 |
| strtod |
√ |
√ |
√ 【注】 |
| _tcstod |
√ |
√ |
√ 【注】 |
| wcstod |
√ |
√ |
√ 【注】 |
相关链接:
• atof • _atold • strtod • strtof • strtold • ecvt • fcvt • gcvt
• StrToFloat • StrToFloatDef • FloatToStr • FloatToStrF • FormatFloat
• _matherr • 浮点数异常处理
|
|
|
|