C++ Builder 參考手冊 ? System::Sysutils ? StrToFloatDef
字符串轉(zhuǎn)浮點(diǎn)數(shù)值
頭文件:#include <System.SysUtils.hpp>
命名空間:System::Sysutils
函數(shù)原型:
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);
參數(shù):
- S:字符串罩引;
- Default:默認(rèn)值耗式;
- AFormatSettings:地區(qū)格式昔馋;
返回值:
- 浮點(diǎn)類型數(shù)值接校,如果轉(zhuǎn)換失敗猛频,函數(shù)返回參數(shù) Default 的值;
- 如果沒有 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ù)是線程安全的越锈。
例:
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(StrToFloatDef(L"1234.56789", 0)); // 當(dāng)前地區(qū) (中國) 小數(shù)點(diǎn)必須用 '.'
ShowFloat(StrToFloatDef(L"1234,56789", 0)); // 小數(shù)點(diǎn)不能用其他字符
TFormatSettings fs;
// fs = TFormatSettings::Create(L"vi"); // 越南
fs = TFormatSettings::Create(L"fr"); // 法國
ShowFloat(StrToFloatDef(L"1234,56789", 0, fs)); // 法國和越南小數(shù)點(diǎn)必須用逗號 ','
ShowFloat(StrToFloatDef(L"1234.56789", 0, fs)); // 如果用了小圓點(diǎn) '.' 就無法識別甘凭,返回默認(rèn)值:0
fs = TFormatSettings::Create(L"en_US"); // 美國
ShowFloat(StrToFloatDef(L"1234.56789", 0, fs)); // 美國的小數(shù)點(diǎn)用小圓點(diǎn)和中國的一樣 '.'
ShowFloat(StrToFloatDef(L"1234,56789", 0, fs)); // 小數(shù)點(diǎn)不能用其他字符
Sysutils::FormatSettings.DecimalSeparator = L','; // 把默認(rèn)的小數(shù)點(diǎn)改成逗號 ','
ShowFloat(StrToFloatDef(L"1234,56789", 0)); // 默認(rèn)的小數(shù)點(diǎn)需要用逗號 ','
ShowFloat(StrToFloatDef(L"1234.56789", 0)); // 這時(shí)候就不識別小圓點(diǎn) '.' 返回默認(rèn)值:0
Sysutils::GetFormatSettings(); // 格式恢復(fù)默認(rèn)值 - 當(dāng)前地區(qū) (中國) 的格式
ShowFloat(StrToFloatDef(L"1234.56789", 0)); // 當(dāng)前地區(qū) (中國) 小數(shù)點(diǎn)必須用 '.'
ShowFloat(StrToFloatDef(L"1234,56789", 0)); // 小數(shù)點(diǎn)不能用其他字符
}
運(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 ? StrToFloatDef