CurrToStrF - C++ Builder

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


货币型数值转字符串,使用浮点数格式

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

System::UnicodeString __fastcall CurrToStrF(System::Currency Value, TFloatFormat Format, int Digits);
System::UnicodeString __fastcall CurrToStrF(System::Currency Value, TFloatFormat Format, int Digits, const TFormatSettings &AFormatSettings);

参数:

  • Value:货币型变量;
  • Format:浮点数格式:
    • ffGeneral:生成尽可能短的字符串,忽略 Digits 参数;
    • ffExponent:使用科学计数法,Digits 为指数的位数,不是小数点后保留的位数;
    • ffFixed:小数点后保留 Digits 位;
    • ffNumber:小数点后保留 Digits 位,并且整数部使用千位分隔符;
    • ffCurrency:使用货币型字符串,小数点后保留 Digits 位;
  • Digits:根据 Format 参数使用;
  • AFormatSettings:地区格式;

返回值:

  • 参数 Value 转为字符串;
  • 地区格式:这个函数使用了地区格式的 DecimalSeparator 作为小数点、ThousandSeparator 作为千位分隔符、CurrencyString 作为货币符号、CurrencyFormat 控制货币符号放在数字前面或是后面、NegCurrFormat 作为货币负数的格式,不同的地区可能会使用不同的字符当做小数点和千位分隔符,中国和大多数国家一样使用小圆点作为小数点、逗号作为千位分隔符,但是有的国家 - 例如法国:使用逗号当做小数点、空格当做千位分隔符,如果程序在法国和越南等国家的电脑上运行,默认情况所有的小数点都会使用逗号的,包括浮点数,不仅仅是货币型,程序国际化时要特别注意;
  • 如果没有 AFormatSettings 参数,使用当前地区格式;
  • 如果有 AFormatSettings 参数,使用这个参数的格式,并且使用这个参数可以随意设定一个字符当做小数点;
  • 可以使用全局变量 System::Sysutils::FormatSettings 修改默认的格式;
  • 没有 AFormatSettings 参数的 CurrToStrF 函数不是线程安全的,因为使用了全局变量作为默认的地区格式;带有 AFormatSettings 参数的函数是线程安全的。

例:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    System::Currency Val = 1234.5678;

    Memo1->Lines->Add(Sysutils::CurrToStrF(Val, ffGeneral , 3));
    Memo1->Lines->Add(Sysutils::CurrToStrF(Val, ffExponent, 3));
    Memo1->Lines->Add(Sysutils::CurrToStrF(Val, ffFixed   , 3));
    Memo1->Lines->Add(Sysutils::CurrToStrF(Val, ffNumber  , 3));
    Memo1->Lines->Add(Sysutils::CurrToStrF(Val, ffCurrency, 3));

    Memo1->Lines->Add(L"--------");
    Memo1->Lines->Add(Sysutils::CurrToStrF(Val, ffNumber  , 3, TFormatSettings::Create(L"en_US"))); // 美国数字
    Memo1->Lines->Add(Sysutils::CurrToStrF(Val, ffCurrency, 3, TFormatSettings::Create(L"en_US"))); // 美国货币

    Memo1->Lines->Add(L"--------");
    Memo1->Lines->Add(Sysutils::CurrToStrF(Val, ffNumber  , 3, TFormatSettings::Create(L"fr_FR"))); // 法国数字
    Memo1->Lines->Add(Sysutils::CurrToStrF(Val, ffCurrency, 3, TFormatSettings::Create(L"fr_FR"))); // 法国货币

    Memo1->Lines->Add(L"--------");
    TFormatSettings fs = TFormatSettings::Create();
    fs.CurrencyString = L"@#";                                       // 货币符号
    fs.ThousandSeparator = L'\'';                                    // 千位分隔符
    fs.DecimalSeparator = L',';                                      // 小数点
    Memo1->Lines->Add(Sysutils::CurrToStrF(Val, ffCurrency, 3, fs)); // 自定义的货币
}

运行结果:

运行结果

相关:

  • System::Sysutils::FormatSettings
  • System::Sysutils::TFormatSettings
  • System::Sysutils::StrToBool
  • System::Sysutils::StrToBoolDef
  • System::Sysutils::TryStrToBool
  • System::Sysutils::BoolToStr
  • System::Sysutils::CurrToStr
  • System::Sysutils::CurrToStrF
  • System::Sysutils::DateTimeToStr
  • System::Sysutils::DateTimeToString
  • System::Sysutils::DateToStr
  • System::Sysutils::FloatToStr
  • System::Sysutils::FloatToStrF
  • System::Sysutils::GUIDToString
  • System::Sysutils::IntToStr
  • System::Sysutils::IntToHex
  • System::Sysutils::TimeToStr
  • System::Sysutils::UIntToStr
  • System::Sysutils
  • System::Currency
  • System
  • std::itoa, std::_itoa, std::_itot, std::_itow
  • std::ltoa, std::_ltoa, std::_ltot, std::_ltow
  • std::ultoa, std::_ultoa, std::_ultot, std::_ultow
  • std::_i64toa, std::_i64tot, std::_i64tow
  • std::_ui64toa, std::_ui64tot, std::_ui64tow
  • std::ecvt, std::_ecvt
  • std::fcvt, std::_fcvt
  • std::gcvt, std::_gcvt

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

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