FloatToText - C++ Builder

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


字符串转为字符串,按照参数给定的格式

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

int __fastcall FloatToText(
    System::WideChar * BufferArg,
    const void *Value,
    TFloatValue ValueType,
    TFloatFormat Format,
    int Precision,
    int Digits);
int __fastcall FloatToText(char * BufferArg,
    const void *Value,
    TFloatValue ValueType,
    TFloatFormat Format,
    int Precision,
    int Digits);
int __fastcall FloatToText(
    System::WideChar * BufferArg,
    const void *Value,
    TFloatValue ValueType,
    TFloatFormat Format,
    int Precision,
    int Digits,
    const TFormatSettings &AFormatSettings);
int __fastcall FloatToText(
    char * BufferArg,
    const void *Value,
    TFloatValue ValueType,
    TFloatFormat Format,
    int Precision,
    int Digits,
    const TFormatSettings &AFormatSettings);

参数:

  • BufferArg:用于返回生成的字符串;
  • Value:需要分解的浮点数,System::Currency 或者 System::Extended 类型的变量地址;
  • ValueType:指定参数 Value 的类型:
    • fvExtended:参数 Value 是 System::Extended 类型的;
    • fvCurrency:参数 Value 是 System::Currency 类型的;
  • Format:浮点数格式:
    • ffGeneral:生成尽可能短的字符串,忽略 Digits 参数;
    • ffExponent:使用科学计数法,Digits 为指数的位数,不是小数点后保留的位数;
    • ffFixed:小数点后保留 Digits 位;
    • ffNumber:小数点后保留 Digits 位,并且整数部使用千位分隔符;
    • ffCurrency:使用货币型字符串,小数点后保留 Digits 位;
  • Precision:精度,保留有效数字的位数;
  • Digits:根据 Format 参数使用;
  • AFormatSettings:地区格式;

返回值:

  • 函数返回生成的字符串的长度,生成的字符串通过参数 BufferArg 返回,
    • 文档并没有说明 BufferArg 具体需要的字节数,在调用函数之前必须已经分配足够的内存,可以根据参数估计或分配多一点内存 (比如 100 个字符长度);
    • 通过参数 BufferArg 返回的字符串没有结束符,根据源码的使用情况分析,这个函数用于连续输出字符到缓存,这样做可以提高效率,不要忘记在使用生成的字符串之前要在字符串长度位置加一个结束符;
  • char * 类型的 BufferArg 参数的函数为过时的 ANSI 版本的函数,直接使用时可能有警告或报错,已经移动到 System.AnsiStrings.hpp 头文件里面了;
  • 其他描述请参考 FloatToStrF

例:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    System::WideChar Buffer[100];
    System::Extended Value = -1234.56789;
    int n = FloatToText(Buffer, &Value, fvExtended, ffNumber, 8, 3);
    Buffer[n] = 0;
    Memo1->Lines->Add(Buffer);
}

运行结果:

运行结果

相关:

  • System::Sysutils::FloatToStr
  • System::Sysutils::FloatToStrF
  • System::Sysutils::FloatToText
  • System::Sysutils::FloatToTextFmt
  • System::Sysutils::FloatToDecimal
  • System::Sysutils::FloatToCurr
  • System::Sysutils::TryFloatToCurr
  • System::Sysutils::TextToFloat
  • System::Sysutils::StrToFloat
  • System::Sysutils::StrToFloatDef
  • System::Sysutils::TryStrToFloat
  • System::Sysutils::StrToCurr
  • System::Sysutils::StrToCurrDef
  • System::Sysutils::TryStrToCurr
  • System::Sysutils::CurrToStr
  • System::Sysutils::CurrToStrF
  • System::Sysutils::FormatFloat
  • System::Sysutils::FormatCurr
  • System::Sysutils::TFloatRec
  • System::Sysutils::TFloatValue
  • System::Sysutils::FormatSettings
  • System::Sysutils::TFormatSettings
  • System::Sysutils::StrToBool
  • System::Sysutils::StrToBoolDef
  • System::Sysutils::TryStrToBool
  • System::Sysutils::BoolToStr
  • System::Sysutils::DateTimeToStr
  • System::Sysutils::DateTimeToString
  • System::Sysutils::DateToStr
  • 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 ➙ FloatToText

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