Number
Humanizing functions for numbers.
apnumber(value)
Converts an integer to Associated Press style.
Examples:
>>> apnumber(0)
'zero'
>>> apnumber(5)
'five'
>>> apnumber(10)
'10'
>>> apnumber("7")
'seven'
>>> apnumber("foo")
'foo'
>>> apnumber(None)
'None'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
(int, float, str)
|
Integer to convert. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
For numbers 0-9, the number spelled out. Otherwise, the number. This always
returns a string unless the value was not |
Source code in .tox/docs/lib/python3.13/site-packages/humanize/number.py
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
|
clamp(value, format='{:}', floor=None, ceil=None, floor_token='<', ceil_token='>')
Returns number with the specified format, clamped between floor and ceil.
If the number is larger than ceil or smaller than floor, then the respective limit will be returned, formatted and prepended with a token specifying as such.
Examples:
>>> clamp(123.456)
'123.456'
>>> clamp(0.0001, floor=0.01)
'<0.01'
>>> clamp(0.99, format="{:.0%}", ceil=0.99)
'99%'
>>> clamp(0.999, format="{:.0%}", ceil=0.99)
'>99%'
>>> clamp(1, format=intword, floor=1e6, floor_token="under ")
'under 1.0 million'
>>> clamp(None) is None
True
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
(int, float)
|
Input number. |
required |
format
|
str OR callable
|
Can either be a formatting string, or a callable function that receives value and returns a string. |
'{:}'
|
floor
|
(int, float)
|
Smallest value before clamping. |
None
|
ceil
|
(int, float)
|
Largest value before clamping. |
None
|
floor_token
|
str
|
If value is smaller than floor, token will be prepended to output. |
'<'
|
ceil_token
|
str
|
If value is larger than ceil, token will be prepended to output. |
'>'
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
Formatted number. The output is clamped between the indicated floor and ceil. If the number is larger than ceil or smaller than floor, the output will be prepended with a token indicating as such. |
Source code in .tox/docs/lib/python3.13/site-packages/humanize/number.py
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 |
|
fractional(value)
Convert to fractional number.
There will be some cases where one might not want to show ugly decimal places for floats and decimals.
This function returns a human-readable fractional number in form of fractions and mixed fractions.
Pass in a string, or a number or a float, and this function returns:
- a string representation of a fraction
- or a whole number
- or a mixed fraction
- or the str output of the value, if it could not be converted
Examples:
>>> fractional(0.3)
'3/10'
>>> fractional(1.3)
'1 3/10'
>>> fractional(float(1/3))
'1/3'
>>> fractional(1)
'1'
>>> fractional("ten")
'ten'
>>> fractional(None)
'None'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
(int, float, str)
|
Integer to convert. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
Fractional number as a string. |
Source code in .tox/docs/lib/python3.13/site-packages/humanize/number.py
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 |
|
intcomma(value, ndigits=None)
Converts an integer to a string containing commas every three digits.
For example, 3000 becomes "3,000" and 45000 becomes "45,000". To maintain some
compatibility with Django's intcomma
, this function also accepts floats.
Examples:
>>> intcomma(100)
'100'
>>> intcomma("1000")
'1,000'
>>> intcomma(1_000_000)
'1,000,000'
>>> intcomma(1_234_567.25)
'1,234,567.25'
>>> intcomma(1234.5454545, 2)
'1,234.55'
>>> intcomma(14308.40, 1)
'14,308.4'
>>> intcomma("14308.40", 1)
'14,308.4'
>>> intcomma(None)
'None'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
(int, float, str)
|
Integer or float to convert. |
required |
ndigits
|
(int, None)
|
Digits of precision for rounding after the decimal point. |
None
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
String containing commas every three digits. |
Source code in .tox/docs/lib/python3.13/site-packages/humanize/number.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
|
intword(value, format='%.1f')
Converts a large integer to a friendly text representation.
Works best for numbers over 1 million. For example, 1_000_000 becomes "1.0 million", 1200000 becomes "1.2 million" and "1_200_000_000" becomes "1.2 billion". Supports up to decillion (33 digits) and googol (100 digits).
Examples:
>>> intword("100")
'100'
>>> intword("12400")
'12.4 thousand'
>>> intword("1000000")
'1.0 million'
>>> intword(1_200_000_000)
'1.2 billion'
>>> intword(8100000000000000000000000000000000)
'8.1 decillion'
>>> intword(None)
'None'
>>> intword("1234000", "%0.3f")
'1.234 million'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
(int, float, str)
|
Integer to convert. |
required |
format
|
str
|
To change the number of decimal or general format of the number portion. |
'%.1f'
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
Friendly text representation as a string, unless the value passed could not
be coaxed into an |
Source code in .tox/docs/lib/python3.13/site-packages/humanize/number.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
|
metric(value, unit='', precision=3)
Return a value with a metric SI unit-prefix appended.
Examples:
>>> metric(1500, "V")
'1.50 kV'
>>> metric(2e8, "W")
'200 MW'
>>> metric(220e-6, "F")
'220 μF'
>>> metric(1e-14, precision=4)
'10.00 f'
The unit prefix is always chosen so that non-significant zero digits are required.
i.e. 123,000
will become 123k
instead of 0.123M
and 1,230,000
will become
1.23M
instead of 1230K
. For numbers that are either too huge or too tiny to
represent without resorting to either leading or trailing zeroes, it falls back to
scientific()
.
>>> metric(1e40)
'1.00 x 10⁴⁰'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
(int, float)
|
Input number. |
required |
unit
|
str
|
Optional base unit. |
''
|
precision
|
int
|
The number of digits the output should contain. |
3
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
|
Source code in .tox/docs/lib/python3.13/site-packages/humanize/number.py
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 |
|
ordinal(value, gender='male')
Converts an integer to its ordinal as a string.
For example, 1 is "1st", 2 is "2nd", 3 is "3rd", etc. Works for any integer or
anything int()
will turn into an integer. Anything else will return the output
of str(value).
Examples:
>>> ordinal(1)
'1st'
>>> ordinal(1002)
'1002nd'
>>> ordinal(103)
'103rd'
>>> ordinal(4)
'4th'
>>> ordinal(12)
'12th'
>>> ordinal(101)
'101st'
>>> ordinal(111)
'111th'
>>> ordinal("something else")
'something else'
>>> ordinal([1, 2, 3]) == "[1, 2, 3]"
True
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
(int, str, float)
|
Integer to convert. |
required |
gender
|
str
|
Gender for translations. Accepts either "male" or "female". |
'male'
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
Ordinal string. |
Source code in .tox/docs/lib/python3.13/site-packages/humanize/number.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
|
scientific(value, precision=2)
Return number in string scientific notation z.wq x 10ⁿ.
Examples:
>>> scientific(float(0.3))
'3.00 x 10⁻¹'
>>> scientific(int(500))
'5.00 x 10²'
>>> scientific(-1000)
'-1.00 x 10³'
>>> scientific(1000, 1)
'1.0 x 10³'
>>> scientific(1000, 3)
'1.000 x 10³'
>>> scientific("99")
'9.90 x 10¹'
>>> scientific("foo")
'foo'
>>> scientific(None)
'None'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
(int, float, str)
|
Input number. |
required |
precision
|
int
|
Number of decimal for first part of the number. |
2
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
Number in scientific notation z.wq x 10ⁿ. |
Source code in .tox/docs/lib/python3.13/site-packages/humanize/number.py
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 |
|