主页C++ Builder 资料C++ Builder 参考手册System 字符串StrToFloat
C++ Builder 串口控件
C++ Builder 编程技巧
C++ Builder 操作指南
C++ Builder 参考手册
基础知识
cfloat 浮点数
cmath 数学函数
cstdlib 标准库函数
System 字符串
 • UnicodeString
 • UTF8String
 • TStringList
 • TStrings
 • String
 • AnsiString
 • AnsiStringBase
 • AnsiStringT
 • RawByteString
 • UCS4String
 • WideString
 • EnumToStr
 • FloatToStr
 • FloatToStrF
 • FormatFloat
 • IntToStr
 • UIntToStr
 • IntToHex
 • StrToEnum
 • StrToFloat
 • StrToFloatDef
 • StrToInt
 • StrToIntDef
 • StrToInt64
 • StrToInt64Def
 • StrToUInt64
 • StrToUInt64Def
 • TFloatFormat
 • GetUnicodeCategory
 • IMLangCodePages
 • StringReplace
System 日期和时间
System.Math.hpp 数学函数
其他数据类型
VCL 基础类
VCL 应用程序
Pictures 图片
Graphics 绘图
Additional 控件
System 控件
A ~ Z 字母顺序排列的目录
网友留言/技术支持
StrToFloat - 字符串转浮点数,失败抛出异常

函数原型:

Extended __fastcall StrToFloat(const UnicodeString S);
Extended __fastcall StrToFloat(const UnicodeString S, const TFormatSettings &AFormatSettings);

 

头文件:

#include <System.SysUtils.hpp> (XE2 之后),#include <SysUtils.hpp> (XE 之前)

 

参数:

S: 为字符串,支持小数格式 L"-123.456" 和科学计数法格式 L"-1.235e02",不支持千分位分割符,不支持 NAN、+INF、-INF。
AFormatSettings: TFormatSettings 类型转换字符串的格式,如果没有提供这个参数,会使用全局对象 FormatSettings 作为这个格式参数。

没有 AFormatSettings 参数的函数使用了全局对象 FormatSettings,不是线程安全的;
含有 AFormatSettings 参数的函数,没有使用全局对象,是线程安全的。

 

返回值:

转换之后的浮点数,是 Extended 类型,即 long double 类型的,可以给 float 或 double 赋值,会降低精度。
如果转换失败,会抛出 EConvertError 异常,需要用 try 捕获异常。

 

例:

try
{
  double v = StrToFloat(Edit1->Text);
}
catch(Exception &E)
{
  ShowMessage(L"您输入的 \"" + Edit1->Text + L"\" 不是有效的浮点数!\r\n错误信息:" + E.Message);
}

 

相关链接:

StrToFloatDefFloatToStrFloatToStrFFormatFloat
atof_atoldstrtodstrtofstrtoldecvtfcvtgcvt

◤上一页:StrToEnum下一页:StrToFloatDef

C++ 爱好者 -- Victor Chen 的个人网站 www.cppfans.com 辽ICP备11016859号