FloatToDecimal - C++ Builder

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


浮点数分解为有效数字、指数和符号

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

void __fastcall FloatToDecimal(TFloatRec &Result, const void *Value, TFloatValue ValueType, int Precision, int Decimals);

参数:

  • Result:用于返回浮点数分解之后的各个部分;
  • Value:需要分解的浮点数,System::Currency 或者 System::Extended 类型的变量地址;
  • ValueType:指定参数 Value 的类型:
    • fvExtended:参数 Value 是 System::Extended 类型的;
    • fvCurrency:参数 Value 是 System::Currency 类型的;
  • Precision:精度:
    • 如果参数是 System::Extended 类型的数值,表示输出的有效数字位数 1 ~ 18,
    • 如果参数是 System::Currency 类型的数值,忽略这个参数,始终认为是 19;
  • Decimals:Value 的小数点后面保留的位数,如果不想破坏这个源值,可以指定一个大数比如 9999,这样就可以按照 Precision 参数的精度输出有效位数,例如后面的例子:例2

返回值:

  • Result:用于返回浮点数分解之后的各个部分
    • Exponent:整数部分的位数;
    • Negative:负号 true:负数,false:正数;
    • Digits:有效数字;

例1:-1234.56789 小数点后保留 3 位,然后取 8 位有效数字

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    TFloatRec Rec;
    System::Extended Value = -1234.56789;
    FloatToDecimal(Rec, &Value, fvExtended, 8, 3);

    Memo1->Lines->Add(L"整数部分:" + IntToStr(Rec.Exponent));
    Memo1->Lines->Add(L"负号:" + BoolToStr(Rec.Negative,true));
    UnicodeString s = L"有效数字:";
    int n = Rec.Digits.Size();
    for(int i=0; iLines->Add(s);
}

首先:-1234.56789 小数点后保留 3 位: -1234.568
然后:取 8 位有效数字,由于上一步只剩下 7 位了,所以只有 7 位:1234568,整数部分 4 位,有负号。

运行结果

例2:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    TFloatRec Rec;
    System::Extended Value = 0.000000123456789;
    FloatToDecimal(Rec, &Value, fvExtended, 8, 9999);

    Memo1->Lines->Add(L"整数部分:" + IntToStr(Rec.Exponent));
    Memo1->Lines->Add(L"负号:" + BoolToStr(Rec.Negative,true));
    UnicodeString s = L"有效数字:";
    int n = Rec.Digits.Size();
    for(int i=0; iLines->Add(s);
}

运行结果:

运行结果

相关:

  • System::Sysutils::TFloatRec
  • System::Sysutils::TFloatValue
  • System::Sysutils::FloatToDecimal
  • System::Sysutils::FloatToCurr
  • System::Sysutils::TryFloatToCurr
  • System::Sysutils::FloatToDateTime
  • System::Sysutils::TryFloatToDateTime
  • System::Sysutils::CurrToStr
  • System::Sysutils::CurrToStrF
  • System::Sysutils::FloatToStr
  • System::Sysutils::FloatToStrF
  • 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 ➙ FloatToDecimal

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