StrToFloatDef - C++ Builder

C++ Builder 参考手册 ➙ System::Sysutils ➙ StrToFloatDef


字符串转浮点数值

头文件:#include
命名空间:System::Sysutils
函数原型:

System::Extended __fastcall StrToFloatDef(const System::UnicodeString S, const System::Extended Default);
System::Extended __fastcall StrToFloatDef(const System::UnicodeString S, const System::Extended Default, const TFormatSettings &AFormatSettings);

参数:

  • S:字符串;
  • Default:默认值;
  • AFormatSettings:地区格式;

返回值:

  • 浮点类型数值,如果转换失败,函数返回参数 Default 的值;
  • 如果没有 AFormatSettings 参数,小数点使用全局变量 FormatSettings.DecimalSeparator,
    不同国家或地区的小数点也不同,例如:中国和美国使用小圆点 '.',法国和越南使用逗号 ',';
    如果 FormatSettings 被修改,可以用 Sysutils::GetFormatSettings(); 恢复默认值为本地格式;
  • 如果有 AFormatSettings 参数,小数点使用 AFormatSettings.DecimalSeparator;
  • 函数 StrToFloat、StrToFloatDef 和 TryStrToFloat 的区别:
    • StrToFloat 转换失败抛出异常;
    • StrToFloatDef 转换失败返回默认值;
    • TryStrToFloat 转换结果通过参数返回,函数返回值返回是否转换成功;
  • 没有 AFormatSettings 参数的 CurrToStrF 函数不是线程安全的,因为使用了全局变量 FormatSettings 作为默认的地区格式;带有 AFormatSettings 参数的函数是线程安全的。

例:

void TForm1::ShowFloat(double lfValue) // 显示浮点数,不使用地区格式
{
    UnicodeString s;
    Memo1->Lines->Add(s.sprintf(L"%f",lfValue)); // 不使用地区格式
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    ShowFloat(StrToFloatDef(L"1234.56789", 0)); // 当前地区 (中国) 小数点必须用 '.'
    ShowFloat(StrToFloatDef(L"1234,56789", 0)); // 小数点不能用其他字符

    TFormatSettings fs;
//  fs = TFormatSettings::Create(L"vi"); // 越南
    fs = TFormatSettings::Create(L"fr"); // 法国
    ShowFloat(StrToFloatDef(L"1234,56789", 0, fs)); // 法国和越南小数点必须用逗号 ','
    ShowFloat(StrToFloatDef(L"1234.56789", 0, fs)); // 如果用了小圆点 '.' 就无法识别,返回默认值:0

    fs = TFormatSettings::Create(L"en_US");   // 美国
    ShowFloat(StrToFloatDef(L"1234.56789", 0, fs)); // 美国的小数点用小圆点和中国的一样 '.'
    ShowFloat(StrToFloatDef(L"1234,56789", 0, fs)); // 小数点不能用其他字符

    Sysutils::FormatSettings.DecimalSeparator = L','; // 把默认的小数点改成逗号 ','
    ShowFloat(StrToFloatDef(L"1234,56789", 0)); // 默认的小数点需要用逗号 ','
    ShowFloat(StrToFloatDef(L"1234.56789", 0)); // 这时候就不识别小圆点 '.' 返回默认值:0

    Sysutils::GetFormatSettings(); // 格式恢复默认值 - 当前地区 (中国) 的格式
    ShowFloat(StrToFloatDef(L"1234.56789", 0)); // 当前地区 (中国) 小数点必须用 '.'
    ShowFloat(StrToFloatDef(L"1234,56789", 0)); // 小数点不能用其他字符
}

运行结果:

运行结果

相关:

  • System::Sysutils::FloatToStr
  • System::Sysutils::FloatToStrF
  • System::Sysutils::FloatToText
  • System::Sysutils::FloatToTextFmt
  • System::Sysutils::Format
  • System::Sysutils::FormatBuf
  • System::Sysutils::FormatFloat
  • System::Sysutils::StrToBool
  • System::Sysutils::StrToBoolDef
  • System::Sysutils::StrToCurr
  • System::Sysutils::StrToCurrDef
  • System::Sysutils::StrToDate
  • System::Sysutils::StrToDateDef
  • System::Sysutils::StrToDateTime
  • System::Sysutils::StrToDateTimeDef
  • System::Sysutils::StrToFloat
  • System::Sysutils::StrToFloatDef
  • System::Sysutils::StrToInt
  • System::Sysutils::StrToIntDef
  • System::Sysutils::StrToInt64
  • System::Sysutils::StrToInt64Def
  • System::Sysutils::StrToTime
  • System::Sysutils::StrToTimeDef
  • System::Sysutils::StrToUInt
  • System::Sysutils::StrToUIntDef
  • System::Sysutils::StrToUInt64
  • System::Sysutils::StrToUInt64Def
  • System::Sysutils::TimeToStr
  • System::Sysutils::TryStrToBool
  • System::Sysutils::TryStrToCurr
  • System::Sysutils::TryStrToDate
  • System::Sysutils::TryStrToDateTime
  • System::Sysutils::TryStrToFloat
  • System::Sysutils::TryStrToInt
  • System::Sysutils::TryStrToInt64
  • System::Sysutils::TryStrToTime
  • System::Sysutils::TryStrToUInt
  • System::Sysutils::TryStrToUInt64
  • System::Sysutils
  • std::atof, std::_ttof, std::_wtof
  • std::_atold, std::_ttold, std::_wtold
  • std::atoi, std::_ttoi, std::_wtoi
  • std::atol, std::_ttol, std::_wtol
  • std::atoll, std::_ttoll, std::_wtoll
  • std::_atoi64, std::_ttoi64, std::_wtoi64
  • std::strtof, std::_tcstof, std::wcstof
  • std::strtod, std::_tcstod, std::wcstod
  • std::strtold, std::wcstold, std::_strtold, std::_tcstold, std::_wcstold
  • std::strtol, std::_tcstol, std::wcstol
  • std::strtoll, std::_tcstoll, std::wcstoll
  • std::strtoul, std::_tcstoul, std::wcstoul
  • std::strtoull, std::_tcstoull, std::wcstoull

C++ Builder 参考手册 ➙ System::Sysutils ➙ StrToFloatDef

你可能感兴趣的:(StrToFloatDef - C++ Builder)