美文网首页
StrToCurr - C++ Builder

StrToCurr - C++ Builder

作者: 玄坴 | 来源:发表于2022-04-19 09:36 被阅读0次

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


    字符串转货币类型数值

    头文件:#include <System.SysUtils.hpp>
    命名空间:System::Sysutils
    函数原型:

    System::Currency __fastcall StrToCurr(const System::UnicodeString S);
    System::Currency __fastcall StrToCurr(const System::UnicodeString S, const TFormatSettings &AFormatSettings);
    

    参数:

    • S:字符串;
    • AFormatSettings:地区格式;

    返回值:

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

    例:

    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
        Memo1->Lines->Add(StrToCurr(L"10"    ));
        Memo1->Lines->Add(StrToCurr(L"-1.5"  ));
        Memo1->Lines->Add(StrToCurr(L"12.345"));
    
        TFormatSettings fs = TFormatSettings::Create(L"fr_FR"); // 法国
    //  Memo1->Lines->Add(StrToCurr(L"3.14", fs)); // 法国不能用小圆点 . 作小数点
        Memo1->Lines->Add(StrToCurr(L"3,14", fs)); // 法国必须用逗号 , 作小数点
    }
    

    运行结果:

    运行结果

    相关:

    • System::Sysutils::CurrToStr
    • System::Sysutils::CurrToStrF
    • 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
    • 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::SysutilsStrToCurr

    相关文章

      网友评论

          本文标题:StrToCurr - C++ Builder

          本文链接:https://www.haomeiwen.com/subject/shidahtx.html