StrToFloatDef - C++ Builder

2022-04-26  本文已影响0人  玄坴

C++ Builder 参考手册System::SysutilsStrToFloatDef


字符串转浮点数值

头文件:#include <System.SysUtils.hpp>
命名空间: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);

参数:

返回值:


例:

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)); // 小数点不能用其他字符
}

运行结果:

运行结果

相关:


C++ Builder 参考手册System::SysutilsStrToFloatDef

上一篇 下一篇

猜你喜欢

热点阅读