C++ Builder 參考手冊 ? System::Sysutils ? StrToFloat
字符串轉浮點數值
頭文件:#include <System.SysUtils.hpp>
命名空間:System::Sysutils
函數原型:
System::Extended __fastcall StrToFloat(const System::UnicodeString S);
System::Extended __fastcall StrToFloat(const System::UnicodeString S, const TFormatSettings &AFormatSettings);
參數:
- S:字符串;
- AFormatSettings:地區格式;
返回值:
- 浮點類型數值,如果轉換失敗,拋出 EConvertError 異常;
- 如果沒有 AFormatSettings 參數,小數點使用全局變量 FormatSettings.DecimalSeparator,
不同國家或地區的小數點也不同,例如:中國和美國使用小圓點 '.',法國和越南使用逗號 ',';
如果 FormatSettings 被修改,可以用 Sysutils::GetFormatSettings(); 恢復默認值為本地格式; - 如果有 AFormatSettings 參數,小數點使用 AFormatSettings.DecimalSeparator;
- 函數 StrToFloat、StrToFloatDef 和 TryStrToFloat 的區別:
? StrToFloat 轉換失敗拋出異常;
? StrToFloatDef 轉換失敗返回默認值;
? TryStrToFloat 轉換結果通過參數返回,函數返回值返回是否轉換成功; - 沒有 AFormatSettings 參數的 CurrToStrF 函數不是線程安全的,因為使用了全局變量 FormatSettings 作為默認的地區格式;帶有 AFormatSettings 參數的函數是線程安全的。
例:測試地區格式對 StrToFloat 的影響:包括全局變量 FormatSettings 和參數 AFormatSettings
void TForm1::ShowFloat(double lfValue) // 顯示浮點數,不使用地區格式
{
UnicodeString s;
Memo1->Lines->Add(s.sprintf(L"%f",lfValue)); // 不使用地區格式
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ShowFloat(StrToFloat(L"1234.56789")); // 當前地區 (中國)
TFormatSettings fs;
// fs = TFormatSettings::Create(L"vi"); // 越南
fs = TFormatSettings::Create(L"fr"); // 法國
ShowFloat(StrToFloat(L"1234,56789", fs)); // 法國和越南小數點必須用逗號 ','
fs = TFormatSettings::Create(L"en_US"); // 美國
ShowFloat(StrToFloat(L"1234.56789", fs)); // 美國的小數點用小圓點和中國的一樣 '.'
Sysutils::FormatSettings.DecimalSeparator = L','; // 把默認的小數點改成逗號 ','
ShowFloat(StrToFloat(L"1234,56789")); // 默認的小數點需要用逗號 ','
Sysutils::GetFormatSettings(); // 格式恢復默認值 - 當前地區 (中國) 的格式
ShowFloat(StrToFloat(L"1234.56789")); // 當前地區 (中國)
}
運行結果:
運行結果
相關:
- 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
- <cstdlib>
C++ Builder 參考手冊 ? System::Sysutils ? StrToFloat