From 2d981bb133587f976db310a9584820589f5df304 Mon Sep 17 00:00:00 2001 From: Gawain Bolton <45885849+gawain-bolton@users.noreply.github.com> Date: Wed, 6 Mar 2019 22:50:32 +0100 Subject: [PATCH] Add documentation for '%' format type. (#1071) --- doc/syntax.rst | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/doc/syntax.rst b/doc/syntax.rst index fc27c5e2..914ef34a 100644 --- a/doc/syntax.rst +++ b/doc/syntax.rst @@ -264,6 +264,10 @@ The available presentation types for floating-point values are: | | ``'E'`` if the number gets too large. The | | | representations of infinity and NaN are uppercased, too. | +---------+----------------------------------------------------------+ +| ``'%'`` | Fixed point as a percentage. This is similar to ``'f'``, | +| | but the argument is multiplied by 100 and a percent sign | +| | ``%`` is appended. | ++---------+----------------------------------------------------------+ | none | The same as ``'g'``. | +---------+----------------------------------------------------------+ @@ -357,6 +361,13 @@ Replacing ``%+f``, ``%-f``, and ``% f`` and specifying a sign:: format("{:-f}; {:-f}", 3.14, -3.14); // show only the minus -- same as '{:f}; {:f}' // Result: "3.140000; -3.140000" +As a percentage:: + + format("{0:f} or {0:%}", .635); + // Result: "0.635000 or 63.500000%" + format("{:*^{}.{}%}", 1., 15, 2); // With fill, dynamic width and dynamic precision. + // Result: "****100.00%****" + Replacing ``%x`` and ``%o`` and converting the value to different bases:: format("int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", 42); @@ -412,4 +423,3 @@ Padded hex byte with prefix and always prints both hex characters:: 9 9 11 1001 10 A 12 1010 11 B 13 1011 -