TryStrToCurr - C++ Builder

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


字符串转货币类型数值

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

bool __fastcall TryStrToCurr(
    const System::UnicodeString S,
    System::Currency &Value);

bool __fastcall TryStrToCurr(
    const System::UnicodeString S,
    System::Currency &Value,
    const TFormatSettings &AFormatSettings);

参数:

  • S:字符串;
  • Value:输出货币类型数值转换结果;
  • AFormatSettings:地区格式;

返回值:

  • true:通过参数 Value 返回货币型数值,货币类型范围为:-922337203685477.5808 ~ 922337203685477.5807;
  • false:转换失败,数值超范围或者含有不可识别的字符等;
  • 函数 StrToCurr、StrToCurrDef 和 TryStrToCurr 的区别:
    • StrToCurr 转换失败抛出 EConvertError 异常;
    • StrToCurrDef 转换失败返回默认值;
    • TryStrToCurr 转换结果通过参数 Value 返回,函数返回值返回是否转换成功;
  • 地区格式:这个函数使用了地区格式的 DecimalSeparator 成员作为小数点,中国和大多数国家一样使用小圆点作为小数点,但是有的国家 - 例如法国:使用逗号当做小数点,如果程序在法国和越南等国家的电脑上运行,默认情况所有的小数点都会使用逗号,包括浮点数,不仅仅是货币型,程序国际化时要特别注意;
  • 如果没有 AFormatSettings 参数,使用全局变量 FormatSettings 作为地区格式,可以通过修改这个全局变量来修改默认的格式,使用函数 GetFormatSettings 让这个全局变量恢复默认格式为当前地区格式;
  • 如果有 AFormatSettings 参数,使用这个参数的格式,并且使用这个参数可以随意设定一个字符当做小数点,请参考 StrToCurr 和 StrToCurrDef 的例子;
  • 没有 AFormatSettings 参数的函数不是线程安全的,因为使用了全局变量 FormatSettings 作为默认的地区格式;带有 AFormatSettings 参数的函数是线程安全的。

例子:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    System::Currency cy;

    if(TryStrToCurr(L"10", cy))
        Memo1->Lines->Add(cy);
    else
        Memo1->Lines->Add(L"转换失败");

    if(TryStrToCurr(L"-1.5", cy))
        Memo1->Lines->Add(cy);
    else
        Memo1->Lines->Add(L"转换失败");

    if(TryStrToCurr(L"12.345", cy))
        Memo1->Lines->Add(cy);
    else
        Memo1->Lines->Add(L"转换失败");

    TFormatSettings fs = TFormatSettings::Create(L"fr_FR"); // 法国

    if(TryStrToCurr(L"3.14", cy, fs)) // 法国不能用小圆点 . 作小数点
        Memo1->Lines->Add(cy);
    else
        Memo1->Lines->Add(L"转换失败");

    if(TryStrToCurr(L"3,14", cy, fs)) // 法国必须用逗号 , 作小数点
        Memo1->Lines->Add(cy);
    else
        Memo1->Lines->Add(L"转换失败");
}

运行结果:

运行结果

相关:

  • System::Sysutils::CurrToStr
  • System::Sysutils::CurrToStrF
  • System::Sysutils::FormatSettings
  • System::Sysutils::GetFormatSettings
  • System::Sysutils::TFormatSettings
  • 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::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
  • System::Currency
  • System
  • 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 ➙ TryStrToCurr

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