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

運(yùn)行結(jié)果
相關(guān):
- 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