FormatFloat - C++ Builder

C++ Builder 參考手冊 ? System::Sysutils ? FormatFloat


格式化浮點數,把浮點數按照參數給定格式轉成字符串

頭文件:#include <System.SysUtils.hpp>
命名空間:System::Sysutils
函數原型:

System::UnicodeString __fastcall FormatFloat(
    const System::UnicodeString Format,
    System::Extended Value);
System::UnicodeString __fastcall FormatFloat(
    const System::UnicodeString Format,
    System::Extended Value,
    const TFormatSettings &AFormatSettings);

參數:

  • Value:浮點數;
  • Format:格式字符串,詳見本文后面的表格;
  • AFormatSettings:地區(qū)格式;

返回值:

  • 把參數 Value 的值按照參數 Format 的格式轉為字符串;
  • 地區(qū)格式:這個函數使用了地區(qū)格式的 DecimalSeparator 成員作為小數點、ThousandSeparator 成員作為千位分隔符,不同的地區(qū)可能會使用不同的字符當做小數點和千位分隔符,中國和大多數國家一樣使用小圓點作為小數點、逗號作為千位分隔符,但是有的國家 - 例如法國:使用逗號當做小數點、空格當做千位分隔符,如果程序在法國和越南等國家的電腦上運行,默認情況所有的小數點都會使用逗號的,包括浮點數和貨幣型,程序國際化時要特別注意;
  • 如果有 AFormatSettings 參數,使用這個參數的格式;
  • 如果沒有 AFormatSettings 參數,使用 全局變量 System::Sysutils::FormatSettings 作為地區(qū)格式;
  • AFormatSettings 或 System::Sysutils::FormatSettings 的例子請參考 FloatToStrF
  • 沒有 AFormatSettings 參數的函數不是線程安全的,因為使用了全局變量作為默認的地區(qū)格式;帶有 AFormatSettings 參數的函數是線程安全的。

Format 格式:

控制符 說明
0 這個位置必須輸出數字,如果沒有輸出,用 0 填補
# 這個位置如果沒有數字輸出就不輸出,不用填補
. 輸出小數點。格式字符串里面第一個出現的 '.' 認為是小數點位置,其他的 '.' 被忽略
, 使用千分位分割符。格式字符串里面只要 ',' 就會使用千分位分割符,位置和個數不影響輸出效果
E+e+ 使用科學計數法,后面可以緊跟著最多 4 個 '0',表示指數的位數,指數無論是正數還是負數都輸出符號 +-
E-e- 使用科學計數法,后面可以緊跟著最多 4 個 '0',表示指數的位數,指數正數不輸出符號,指數是負數輸出符號 -
'文字'\"文字\" 單引號和雙引號里面的文字直接原樣輸出
; 如果格式字符串里面包含了分號 ';',說明正數、負數和零采用不同的格式,分號 ';' 是分割正數、負數和零的格式之間的分割符:
沒有分割符:所有的數字采用同一種格式;
有1個分割符:前面部分是正數和0,后面部分是負數的格式;
有2個分割符:前面部分是正數;中間是0;后面是負數的格式

格式輸出的例子:

格式↓ 輸出↘ 數值→ 12345.6789 0 -12345.6789
"0.00" 12345.68 0.00 -12345.68
"0.000" 12345.679 0.000 -12345.679
"#,0.00" 12,345.68 0.00 -12,345.68
"#,##0.00" 12,345.68 0.00 -12,345.68
"000000.000" 012345.679 000000.000 -012345.67
"000,000.000" 012,345.679 000,000.000 -012,345.679
"0.000E+00" 1.235E+04 0.000E+00 -1.235E+04
"'正'0.00;'負'0.00;零" 正12345.68 負12345.68
"0" 12346 0 -12346
"0.###" 12345.679 0 -12345.679
"0.######" 12345.6789 0 -12345.6789
"0.000###" 12345.6789 0.000 -12345.6789
"0.000000" 12345.678900 0.000000 -12345.678900

注:小數點和千分位符會被 AFormatSettings 參數或全局變量 System::Sysutils::FormatSettings 的 DecimalSeparator 和 ThousandSeparator 成員替代,而不是始終使用 '.'',',默認情況,采用的是當前地區(qū)的格式,比如在法國,小數點會使用逗號 ',' 而不是小圓點。地區(qū)格式的具體例子請參考 FloatToStrF


相關:

  • 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
  • <cstdlib>

C++ Builder 參考手冊 ? System::Sysutils ? FormatFloat
`

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容