mirror of
https://github.com/fmtlib/fmt.git
synced 2025-07-30 02:37:36 +02:00
Add examples
This commit is contained in:
@ -335,6 +335,16 @@ Aligning the text and specifying a width::
|
|||||||
format("{:*^30}", "centered"); // use '*' as a fill char
|
format("{:*^30}", "centered"); // use '*' as a fill char
|
||||||
// Result: "***********centered***********"
|
// Result: "***********centered***********"
|
||||||
|
|
||||||
|
Dynamic width::
|
||||||
|
|
||||||
|
format("{:<{}}", "left aligned", 30);
|
||||||
|
// Result: "left aligned "
|
||||||
|
|
||||||
|
Dynamic precision::
|
||||||
|
|
||||||
|
format("{:.{}f}", 3.14, 1);
|
||||||
|
// Result: "3.1"
|
||||||
|
|
||||||
Replacing ``%+f``, ``%-f``, and ``% f`` and specifying a sign::
|
Replacing ``%+f``, ``%-f``, and ``% f`` and specifying a sign::
|
||||||
|
|
||||||
format("{:+f}; {:+f}", 3.14, -3.14); // show it always
|
format("{:+f}; {:+f}", 3.14, -3.14); // show it always
|
||||||
@ -350,7 +360,7 @@ Replacing ``%x`` and ``%o`` and converting the value to different bases::
|
|||||||
// Result: "int: 42; hex: 2a; oct: 52; bin: 101010"
|
// Result: "int: 42; hex: 2a; oct: 52; bin: 101010"
|
||||||
// with 0x or 0 or 0b as prefix:
|
// with 0x or 0 or 0b as prefix:
|
||||||
format("int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}", 42);
|
format("int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}", 42);
|
||||||
// Result: "int: 42; hex: 0x2a; oct: 052; bin: 0b101010"
|
// Result: "int: 42; hex: 0x2a; oct: 052; bin: 0b101010"
|
||||||
|
|
||||||
.. ifconfig:: False
|
.. ifconfig:: False
|
||||||
|
|
||||||
@ -359,13 +369,6 @@ Replacing ``%x`` and ``%o`` and converting the value to different bases::
|
|||||||
format("{:,}", 1234567890);
|
format("{:,}", 1234567890);
|
||||||
'1,234,567,890'
|
'1,234,567,890'
|
||||||
|
|
||||||
Expressing a percentage::
|
|
||||||
|
|
||||||
>>> points = 19
|
|
||||||
>>> total = 22
|
|
||||||
Format("Correct answers: {:.2%}") << points/total)
|
|
||||||
'Correct answers: 86.36%'
|
|
||||||
|
|
||||||
Using type-specific formatting::
|
Using type-specific formatting::
|
||||||
|
|
||||||
>>> import datetime
|
>>> import datetime
|
||||||
|
Reference in New Issue
Block a user