Format - C++ Builder

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


格式化輸出到字符串

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

System::UnicodeString __fastcall Format(
    const System::UnicodeString Format,
    const System::TVarRec *Args,
    const int Args_High);

System::UnicodeString __fastcall Format(
    const System::UnicodeString Format,
    const System::TVarRec *Args,
    const int Args_High,
    const TFormatSettings &AFormatSettings);

template <typename... Args,
    class Enable = typename internal::only_if<
        internal::is_TVarRec_compat<
            typename internal::GetType<0, Args...>::type>::value>::type>
System::String Format(const char *fmt, const Args&... args)
{
    System::TVarRec arg_list[] = {args...};
    return Format(System::String(fmt), arg_list, sizeof...(Args)-1);
}

template <typename... Args,
    class Enable = typename internal::only_if<
        internal::is_TVarRec_compat<
            typename internal::GetType<0, Args...>::type>::value>::type>
System::String Format(const System::WideChar *fmt, const Args&... args)
{
    System::TVarRec arg_list[] = {args...};
    return Format(System::String(fmt), arg_list, sizeof...(Args)-1);
}

參數(shù):

  • Format:格式字符串;
  • Args:要輸出的數(shù)據(jù);
  • Args_High:數(shù)據(jù)的個數(shù)減1;
  • fmt:格式字符串;
  • args:要輸出的數(shù)據(jù),
    可變參數(shù)的函數(shù)模板需要 C++ 11 及更新的編譯器支持,
    老版本 Borland 編譯器只能用含有 System::TVarRec * 類型參數(shù)的函數(shù);
  • AFormatSettings:地區(qū)格式;

返回值:

  • 按照參數(shù) fmt 或 Format 的格式生成字符串,函數(shù)返回生成的字符串:
    格式和 <cstdio> 里面的 printf / sprintf 類似,不完全一樣,詳見后面的格式描述和表格;
    如果格式解析錯誤,會拋出 EConvertError 異常,而不是像 printf / sprintf 那樣得到錯誤的結(jié)果;
    和 printf / sprintf 另一個不同:Format 函數(shù)使用地區(qū)格式,而 printf / sprintf 不使用地區(qū)格式;
  • 參數(shù) Args, Args_High 可以使用 ARRAYOFCONST 宏;
  • 地區(qū)格式的例子請參考 FloatToStrF
    如果有 AFormatSettings 參數(shù),使用這個參數(shù)的格式;
    如果沒有 AFormatSettings 參數(shù),使用 全局變量 System::Sysutils::FormatSettings 作為地區(qū)格式;
  • 沒有 AFormatSettings 參數(shù)的函數(shù)不是線程安全的,因為使用了全局變量作為地區(qū)格式;帶有 AFormatSettings 參數(shù)的函數(shù)是線程安全的。

格式字符串的格式:"%" [index ":"] ["-"] [width] ["." prec] type
格式說明符以百分號 % 開頭,接下來的內(nèi)容按順序為:

  • (可選) 參數(shù)序號,后面跟著冒號 ":"
  • (可選) 左對齊說明符,減號 "-"
  • (可選) 寬度,直接寫數(shù)字
  • (可選) 精度說明符,小數(shù)點 "." 精度數(shù)字
  • (必選) 輸出參數(shù)的類型說明符
類型說明符 描述
d 十進(jìn)制 (decimal) 對應(yīng)的參數(shù)必須是整數(shù),
如果包含精度說明,表示輸出整數(shù)至少要有多少位,位數(shù)不足前面補 0
u 無符號十進(jìn)制 (unsigned decimal),其他描述同 d
e 科學(xué)計數(shù)法 (scientific),對應(yīng)的參數(shù)必須是浮點數(shù),
例如 -1.23456E+789 這樣的格式,表示 -1.23456×10789
f 固定位數(shù)的小數(shù) (fixed),對應(yīng)的參數(shù)必須是浮點數(shù),
精度表示小數(shù)點后面的位數(shù),如果沒指定精度,小數(shù)點后保留2位小數(shù)
g 綜合的 (general),對應(yīng)的參數(shù)必須是浮點數(shù),
采用小數(shù)或科學(xué)計數(shù)法,讓輸出最短,
如果沒指定精度,按15處理,
小數(shù)點之后的末尾的0不輸出
n 帶千分位分割符的數(shù)字 (number),對應(yīng)的參數(shù)必須是浮點數(shù),
其他描述同 f,例如 1,234,567.89
m 貨幣 (money),與 n 的區(qū)別:使用的是地區(qū)格式當(dāng)中貨幣相關(guān)的成員
CurrencyString, CurrencyFormat, NegCurrFormat,
ThousandSeparator, DecimalSeparator, CurrencyDecimals 等
p 指針 (pointer),對應(yīng)的參數(shù)必須是指針類型,輸出指針的地址
s 字符串 (string),精度表示最長輸出,超過的部分不輸出
x 十六進(jìn)制 (hexadecimal),對應(yīng)的參數(shù)必須是整數(shù),
精度表示輸出的位數(shù),不足位數(shù)前面補0
  • 類型說明符大寫和小寫都可以,不影響輸出。
  • 浮點數(shù)和貨幣類型使用地區(qū)格式,請參考 FloatToStrF 地區(qū)格式的說明和例子
  • 參數(shù)序號、寬度和精度可以直接用數(shù)字,例如 "%10d",也可以用星號 '*',如果使用了星號,數(shù)值從參數(shù)里面讀取,例如 Format('%*.*f', 8, 2, 123.456) 相當(dāng)于 Format('%8.2f', 123.456)
  • 寬度表示輸出的位數(shù),默認(rèn)為右對齊,如果寬度是負(fù)數(shù)表示左對齊,對齊是用空格補足寬度的;
  • 格式里面的參數(shù)序號影響從這個參數(shù)開始以后的參數(shù),從這個序號開始往下排,一直到遇到下一個指定的參數(shù)序號,例如 "Format('%d %d %0:d %d', 10, 20)" 返回生成的字符串為 "10 20 10 20"

相關(guān):

  • System::Sysutils::Format
  • System::Sysutils::FormatBuf
  • System::Sysutils::FormatCurr
  • System::Sysutils::FormatDateTime
  • System::Sysutils::FormatFloat
  • System::Sysutils::FmtStr
  • System::Sysutils::FmtLoadStr
  • System::Sysutils::StrFmt
  • System::Sysutils::StrLFmt
  • System::Sysutils::WideFormat
  • System::Sysutils::WideFormatBuf
  • System::Sysutils::WideFmtStr
  • System::Sysutils
  • std::printf, std::_tprintf, std::wprintf
  • std::sprintf, std::_stprintf, std::swprintf
  • std::vprintf, std::_vtprintf, std::vwprintf
  • std::vsprintf, std::_vstprintf, std::vswprintf
  • std::snprintf, std::_sntprintf, std::snwprintf
  • std::vsnprintf, std::_vsntprintf, std::vsnwprintf
  • <cstdio>

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

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

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容