1 -- 'f' code formatting, with explicit precision (>= 0). Output always 2 -- has the given number of places after the point; zeros are added if 3 -- necessary to make this true. 4 5 -- zeros 6 %.0f 0 -> 0 7 %.1f 0 -> 0.0 8 %.2f 0 -> 0.00 9 %.3f 0 -> 0.000 10 %.50f 0 -> 0.00000000000000000000000000000000000000000000000000 11 12 -- precision 0; result should never include a . 13 %.0f 1.5 -> 2 14 %.0f 2.5 -> 2 15 %.0f 3.5 -> 4 16 %.0f 0.0 -> 0 17 %.0f 0.1 -> 0 18 %.0f 0.001 -> 0 19 %.0f 10.0 -> 10 20 %.0f 10.1 -> 10 21 %.0f 10.01 -> 10 22 %.0f 123.456 -> 123 23 %.0f 1234.56 -> 1235 24 %.0f 1e49 -> 9999999999999999464902769475481793196872414789632 25 %.0f 9.9999999999999987e+49 -> 99999999999999986860582406952576489172979654066176 26 %.0f 1e50 -> 100000000000000007629769841091887003294964970946560 27 28 -- precision 1 29 %.1f 0.0001 -> 0.0 30 %.1f 0.001 -> 0.0 31 %.1f 0.01 -> 0.0 32 %.1f 0.04 -> 0.0 33 %.1f 0.06 -> 0.1 34 %.1f 0.25 -> 0.2 35 %.1f 0.75 -> 0.8 36 %.1f 1.4 -> 1.4 37 %.1f 1.5 -> 1.5 38 %.1f 10.0 -> 10.0 39 %.1f 1000.03 -> 1000.0 40 %.1f 1234.5678 -> 1234.6 41 %.1f 1234.7499 -> 1234.7 42 %.1f 1234.75 -> 1234.8 43 44 -- precision 2 45 %.2f 0.0001 -> 0.00 46 %.2f 0.001 -> 0.00 47 %.2f 0.004999 -> 0.00 48 %.2f 0.005001 -> 0.01 49 %.2f 0.01 -> 0.01 50 %.2f 0.125 -> 0.12 51 %.2f 0.375 -> 0.38 52 %.2f 1234500 -> 1234500.00 53 %.2f 1234560 -> 1234560.00 54 %.2f 1234567 -> 1234567.00 55 %.2f 1234567.8 -> 1234567.80 56 %.2f 1234567.89 -> 1234567.89 57 %.2f 1234567.891 -> 1234567.89 58 %.2f 1234567.8912 -> 1234567.89 59 60 -- alternate form always includes a decimal point. This only 61 -- makes a difference when the precision is 0. 62 %#.0f 0 -> 0. 63 %#.1f 0 -> 0.0 64 %#.0f 1.5 -> 2. 65 %#.0f 2.5 -> 2. 66 %#.0f 10.1 -> 10. 67 %#.0f 1234.56 -> 1235. 68 %#.1f 1.4 -> 1.4 69 %#.2f 0.375 -> 0.38 70 71 -- if precision is omitted it defaults to 6 72 %f 0 -> 0.000000 73 %f 1230000 -> 1230000.000000 74 %f 1234567 -> 1234567.000000 75 %f 123.4567 -> 123.456700 76 %f 1.23456789 -> 1.234568 77 %f 0.00012 -> 0.000120 78 %f 0.000123 -> 0.000123 79 %f 0.00012345 -> 0.000123 80 %f 0.000001 -> 0.000001 81 %f 0.0000005001 -> 0.000001 82 %f 0.0000004999 -> 0.000000 83 84 -- 'e' code formatting with explicit precision (>= 0). Output should 85 -- always have exactly the number of places after the point that were 86 -- requested. 87 88 -- zeros 89 %.0e 0 -> 0e+00 90 %.1e 0 -> 0.0e+00 91 %.2e 0 -> 0.00e+00 92 %.10e 0 -> 0.0000000000e+00 93 %.50e 0 -> 0.00000000000000000000000000000000000000000000000000e+00 94 95 -- precision 0. no decimal point in the output 96 %.0e 0.01 -> 1e-02 97 %.0e 0.1 -> 1e-01 98 %.0e 1 -> 1e+00 99 %.0e 10 -> 1e+01 100 %.0e 100 -> 1e+02 101 %.0e 0.012 -> 1e-02 102 %.0e 0.12 -> 1e-01 103 %.0e 1.2 -> 1e+00 104 %.0e 12 -> 1e+01 105 %.0e 120 -> 1e+02 106 %.0e 123.456 -> 1e+02 107 %.0e 0.000123456 -> 1e-04 108 %.0e 123456000 -> 1e+08 109 %.0e 0.5 -> 5e-01 110 %.0e 1.4 -> 1e+00 111 %.0e 1.5 -> 2e+00 112 %.0e 1.6 -> 2e+00 113 %.0e 2.4999999 -> 2e+00 114 %.0e 2.5 -> 2e+00 115 %.0e 2.5000001 -> 3e+00 116 %.0e 3.499999999999 -> 3e+00 117 %.0e 3.5 -> 4e+00 118 %.0e 4.5 -> 4e+00 119 %.0e 5.5 -> 6e+00 120 %.0e 6.5 -> 6e+00 121 %.0e 7.5 -> 8e+00 122 %.0e 8.5 -> 8e+00 123 %.0e 9.4999 -> 9e+00 124 %.0e 9.5 -> 1e+01 125 %.0e 10.5 -> 1e+01 126 %.0e 14.999 -> 1e+01 127 %.0e 15 -> 2e+01 128 129 -- precision 1 130 %.1e 0.0001 -> 1.0e-04 131 %.1e 0.001 -> 1.0e-03 132 %.1e 0.01 -> 1.0e-02 133 %.1e 0.1 -> 1.0e-01 134 %.1e 1 -> 1.0e+00 135 %.1e 10 -> 1.0e+01 136 %.1e 100 -> 1.0e+02 137 %.1e 120 -> 1.2e+02 138 %.1e 123 -> 1.2e+02 139 %.1e 123.4 -> 1.2e+02 140 141 -- precision 2 142 %.2e 0.00013 -> 1.30e-04 143 %.2e 0.000135 -> 1.35e-04 144 %.2e 0.0001357 -> 1.36e-04 145 %.2e 0.0001 -> 1.00e-04 146 %.2e 0.001 -> 1.00e-03 147 %.2e 0.01 -> 1.00e-02 148 %.2e 0.1 -> 1.00e-01 149 %.2e 1 -> 1.00e+00 150 %.2e 10 -> 1.00e+01 151 %.2e 100 -> 1.00e+02 152 %.2e 1000 -> 1.00e+03 153 %.2e 1500 -> 1.50e+03 154 %.2e 1590 -> 1.59e+03 155 %.2e 1598 -> 1.60e+03 156 %.2e 1598.7 -> 1.60e+03 157 %.2e 1598.76 -> 1.60e+03 158 %.2e 9999 -> 1.00e+04 159 160 -- omitted precision defaults to 6 161 %e 0 -> 0.000000e+00 162 %e 165 -> 1.650000e+02 163 %e 1234567 -> 1.234567e+06 164 %e 12345678 -> 1.234568e+07 165 %e 1.1 -> 1.100000e+00 166 167 -- alternate form always contains a decimal point. This only makes 168 -- a difference when precision is 0. 169 170 %#.0e 0.01 -> 1.e-02 171 %#.0e 0.1 -> 1.e-01 172 %#.0e 1 -> 1.e+00 173 %#.0e 10 -> 1.e+01 174 %#.0e 100 -> 1.e+02 175 %#.0e 0.012 -> 1.e-02 176 %#.0e 0.12 -> 1.e-01 177 %#.0e 1.2 -> 1.e+00 178 %#.0e 12 -> 1.e+01 179 %#.0e 120 -> 1.e+02 180 %#.0e 123.456 -> 1.e+02 181 %#.0e 0.000123456 -> 1.e-04 182 %#.0e 123456000 -> 1.e+08 183 %#.0e 0.5 -> 5.e-01 184 %#.0e 1.4 -> 1.e+00 185 %#.0e 1.5 -> 2.e+00 186 %#.0e 1.6 -> 2.e+00 187 %#.0e 2.4999999 -> 2.e+00 188 %#.0e 2.5 -> 2.e+00 189 %#.0e 2.5000001 -> 3.e+00 190 %#.0e 3.499999999999 -> 3.e+00 191 %#.0e 3.5 -> 4.e+00 192 %#.0e 4.5 -> 4.e+00 193 %#.0e 5.5 -> 6.e+00 194 %#.0e 6.5 -> 6.e+00 195 %#.0e 7.5 -> 8.e+00 196 %#.0e 8.5 -> 8.e+00 197 %#.0e 9.4999 -> 9.e+00 198 %#.0e 9.5 -> 1.e+01 199 %#.0e 10.5 -> 1.e+01 200 %#.0e 14.999 -> 1.e+01 201 %#.0e 15 -> 2.e+01 202 %#.1e 123.4 -> 1.2e+02 203 %#.2e 0.0001357 -> 1.36e-04 204 205 -- 'g' code formatting. 206 207 -- zeros 208 %.0g 0 -> 0 209 %.1g 0 -> 0 210 %.2g 0 -> 0 211 %.3g 0 -> 0 212 %.4g 0 -> 0 213 %.10g 0 -> 0 214 %.50g 0 -> 0 215 %.100g 0 -> 0 216 217 -- precision 0 doesn't make a lot of sense for the 'g' code (what does 218 -- it mean to have no significant digits?); in practice, it's interpreted 219 -- as identical to precision 1 220 %.0g 1000 -> 1e+03 221 %.0g 100 -> 1e+02 222 %.0g 10 -> 1e+01 223 %.0g 1 -> 1 224 %.0g 0.1 -> 0.1 225 %.0g 0.01 -> 0.01 226 %.0g 1e-3 -> 0.001 227 %.0g 1e-4 -> 0.0001 228 %.0g 1e-5 -> 1e-05 229 %.0g 1e-6 -> 1e-06 230 %.0g 12 -> 1e+01 231 %.0g 120 -> 1e+02 232 %.0g 1.2 -> 1 233 %.0g 0.12 -> 0.1 234 %.0g 0.012 -> 0.01 235 %.0g 0.0012 -> 0.001 236 %.0g 0.00012 -> 0.0001 237 %.0g 0.000012 -> 1e-05 238 %.0g 0.0000012 -> 1e-06 239 240 -- precision 1 identical to precision 0 241 %.1g 1000 -> 1e+03 242 %.1g 100 -> 1e+02 243 %.1g 10 -> 1e+01 244 %.1g 1 -> 1 245 %.1g 0.1 -> 0.1 246 %.1g 0.01 -> 0.01 247 %.1g 1e-3 -> 0.001 248 %.1g 1e-4 -> 0.0001 249 %.1g 1e-5 -> 1e-05 250 %.1g 1e-6 -> 1e-06 251 %.1g 12 -> 1e+01 252 %.1g 120 -> 1e+02 253 %.1g 1.2 -> 1 254 %.1g 0.12 -> 0.1 255 %.1g 0.012 -> 0.01 256 %.1g 0.0012 -> 0.001 257 %.1g 0.00012 -> 0.0001 258 %.1g 0.000012 -> 1e-05 259 %.1g 0.0000012 -> 1e-06 260 261 -- precision 2 262 %.2g 1000 -> 1e+03 263 %.2g 100 -> 1e+02 264 %.2g 10 -> 10 265 %.2g 1 -> 1 266 %.2g 0.1 -> 0.1 267 %.2g 0.01 -> 0.01 268 %.2g 0.001 -> 0.001 269 %.2g 1e-4 -> 0.0001 270 %.2g 1e-5 -> 1e-05 271 %.2g 1e-6 -> 1e-06 272 %.2g 1234 -> 1.2e+03 273 %.2g 123 -> 1.2e+02 274 %.2g 12.3 -> 12 275 %.2g 1.23 -> 1.2 276 %.2g 0.123 -> 0.12 277 %.2g 0.0123 -> 0.012 278 %.2g 0.00123 -> 0.0012 279 %.2g 0.000123 -> 0.00012 280 %.2g 0.0000123 -> 1.2e-05 281 282 -- bad cases from http://bugs.python.org/issue9980 283 %.12g 38210.0 -> 38210 284 %.12g 37210.0 -> 37210 285 %.12g 36210.0 -> 36210 286 287 -- alternate g formatting: always include decimal point and 288 -- exactly <precision> significant digits. 289 %#.0g 0 -> 0. 290 %#.1g 0 -> 0. 291 %#.2g 0 -> 0.0 292 %#.3g 0 -> 0.00 293 %#.4g 0 -> 0.000 294 295 %#.0g 0.2 -> 0.2 296 %#.1g 0.2 -> 0.2 297 %#.2g 0.2 -> 0.20 298 %#.3g 0.2 -> 0.200 299 %#.4g 0.2 -> 0.2000 300 %#.10g 0.2 -> 0.2000000000 301 302 %#.0g 2 -> 2. 303 %#.1g 2 -> 2. 304 %#.2g 2 -> 2.0 305 %#.3g 2 -> 2.00 306 %#.4g 2 -> 2.000 307 308 %#.0g 20 -> 2.e+01 309 %#.1g 20 -> 2.e+01 310 %#.2g 20 -> 20. 311 %#.3g 20 -> 20.0 312 %#.4g 20 -> 20.00 313 314 %#.0g 234.56 -> 2.e+02 315 %#.1g 234.56 -> 2.e+02 316 %#.2g 234.56 -> 2.3e+02 317 %#.3g 234.56 -> 235. 318 %#.4g 234.56 -> 234.6 319 %#.5g 234.56 -> 234.56 320 %#.6g 234.56 -> 234.560 321 322 -- repr formatting. Result always includes decimal point and at 323 -- least one digit after the point, or an exponent. 324 %r 0 -> 0.0 325 %r 1 -> 1.0 326 327 %r 0.01 -> 0.01 328 %r 0.02 -> 0.02 329 %r 0.03 -> 0.03 330 %r 0.04 -> 0.04 331 %r 0.05 -> 0.05 332 333 -- values >= 1e16 get an exponent 334 %r 10 -> 10.0 335 %r 100 -> 100.0 336 %r 1e15 -> 1000000000000000.0 337 %r 9.999e15 -> 9999000000000000.0 338 %r 9999999999999998 -> 9999999999999998.0 339 %r 9999999999999999 -> 1e+16 340 %r 1e16 -> 1e+16 341 %r 1e17 -> 1e+17 342 343 -- as do values < 1e-4 344 %r 1e-3 -> 0.001 345 %r 1.001e-4 -> 0.0001001 346 %r 1.0000000000000001e-4 -> 0.0001 347 %r 1.000000000000001e-4 -> 0.0001000000000000001 348 %r 1.00000000001e-4 -> 0.000100000000001 349 %r 1.0000000001e-4 -> 0.00010000000001 350 %r 1e-4 -> 0.0001 351 %r 0.99999999999999999e-4 -> 0.0001 352 %r 0.9999999999999999e-4 -> 9.999999999999999e-05 353 %r 0.999999999999e-4 -> 9.99999999999e-05 354 %r 0.999e-4 -> 9.99e-05 355 %r 1e-5 -> 1e-05 356