C++ Builder 參考手冊 ➙ System::Sysutils ➙ FloatToDecimal
浮點數分解爲有效數字、指數和符號
頭文件:#include <System.SysUtils.hpp>
命名空間: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; i<n; i++)
s.cat_sprintf(L"%c", Rec.Digits[i]);
Memo1->Lines->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; i<n; i++)
s.cat_sprintf(L"%c", Rec.Digits[i]);
Memo1->Lines->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
- <cstdlib>
C++ Builder 參考手冊 ➙ System::Sysutils ➙ FloatToDecimal