github fmt库语法+范例(fmt version :7.0.1) (3)

2.6、%x和%o并将值转换为不同的进制:

fmt::format("int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", 42); // Result: "int: 42; hex: 2a; oct: 52; bin: 101010" // with 0x or 0 or 0b as prefix: fmt::format("int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}", 42); // Result: "int: 42; hex: 0x2a; oct: 052; bin: 0b101010"

2.7、用前缀填充十六进制字节,并始终打印两个十六进制字符:

fmt::format("{:#04x}", 0); // Result: "0x00"

2.8、使用Unicode填充的方框图:

fmt::print( "┌{0:─^{2}}┐\n" "│{1: ^{2}}│\n" "└{0:─^{2}}┘\n", "", "Hello, world!", 20);

输出结果:

┌────────────────────┐ │ Hello, world!│ └────────────────────┘

2.9、使用特定于类型的格式:  

#include <fmt/chrono.h> auto t = tm(); t.tm_year = 2010 - 1900; t.tm_mon = 6; t.tm_mday = 4; t.tm_hour = 12; t.tm_min = 15; t.tm_sec = 58; fmt::print("{:%Y-%m-%d %H:%M:%S}", t); // Prints: 2010-08-04 12:15:58

2.10、使用逗号作为千位分隔符:

#include <fmt/locale.h> auto s = fmt::format(std::locale("en_US.UTF-8"), "{:L}", 1234567890); // s == "1,234,567,890"

------------完------------ -

可见fmt的强大之处,类比python的格式化。 c++传统的格式化也该扫进历史的垃圾桶了。
还等什么,赶紧试试吧。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zgyzyg.html