Convert number to human readable text in Python
This Python tutorial we learn how to use the Python humanize package to convert a number value into a human readable and friendly text presentation string in Python.
Table of contents
- Install humanize package
- Convert large number to human readable string
- Convert number to readable string contains commas every three digits
- Convert integer value to Associated Press style string in Python
- Convert integer number to its ordinal string
- Convert number to readable string with format of clamped between floor and ceil
- Convert number to fractional number string
- Convert number to scientific notation string in Python
Install humanize package
Installing the humanize package using the following command.
pip install humanize
Convert large number to human readable string
The following Python program to convert a large number value into a readable string using intword() method of humanize package.
import humanize
number1 = humanize.intword(12300)
number2 = humanize.intword(1_000_000)
number3 = humanize.intword(1_000_000_000)
print(number1)
print(number2)
print(number3)
12.3 thousand
1.0 million
1.0 billion
Convert number to readable string contains commas every three digits
The Python program below to show you how to use the intcomma() method to convert a number into readable string which contains commas every three digits.
import humanize
number1 = humanize.intcomma(1234)
number2 = humanize.intcomma(2_000_000)
number3 = humanize.intcomma(1_234.56)
number4 = humanize.intcomma(1.23456, 2)
number5 = humanize.intcomma(1.23456, 1)
print(number1)
print(number2)
print(number3)
print(number4)
print(number5)
1,234
2,000,000
1,234.56
1.23
1.2
Convert integer value to Associated Press style string in Python
The following Python program to convert number value from 0 to 9 into a readable string as Associated Press style.
import humanize
number1 = humanize.apnumber(0)
number2 = humanize.apnumber(1)
number3 = humanize.apnumber(2.0)
number4 = humanize.apnumber('3')
number5 = humanize.apnumber('toricode')
print(number1)
print(number2)
print(number3)
print(number4)
print(number5)
zero
one
two
three
toricode
Convert integer number to its ordinal string
The following Python program to convert an integer number into its ordinal string using the ordinal() method.
import humanize
number1 = humanize.ordinal(1)
number2 = humanize.ordinal(2)
number3 = humanize.ordinal(3)
number4 = humanize.ordinal(12)
print(number1)
print(number2)
print(number3)
print(number4)
1st
2nd
3rd
12th
Convert number to readable string with format of clamped between floor and ceil
The Python program below to convert number value into a readable string which clamped between floor and ceil with clamp() method.
import humanize
number1 = humanize.clamp(0.0001, floor=0.01)
number2 = humanize.clamp(0.01, floor=0.01)
print(number1)
print(number2)
<0.01
0.01
The example below to use clamp() method with format parameter.
import humanize
number1 = humanize.clamp(0.99, format='{:.0%}', ceil=0.99)
number2 = humanize.clamp(0.999, format='{:.0%}', ceil=0.99)
print(number1)
print(number2)
99%
>99%
The following Python program to use clamp() method with custom token.
import humanize
from humanize.number import intword
number1 = humanize.clamp(1, format=intword, floor=1e6, floor_token='under ')
number2 = humanize.clamp(1000000, format=intword, floor=1e6, floor_token='under ')
print(number1)
print(number2)
under 1.0 million
1.0 million
Convert number to fractional number string
The fractional() method of humanize package to convert decimal number into human readable fractional number as following Python program.
import humanize
number1 = humanize.fractional(0.3)
number2 = humanize.fractional(1.3)
print(number1)
print(number2)
3/10
1 3/10
Convert number to scientific notation string in Python
The program below to convert a number value into string in scientific notation format.
import humanize
number1 = humanize.scientific(0.3)
number2 = humanize.scientific(500)
number3 = humanize.scientific(-1000)
number4 = humanize.scientific(1000, 1)
number5 = humanize.scientific(1000, 3)
print(number1)
print(number2)
print(number3)
print(number4)
print(number5)
3.00 x 10⁻¹
5.00 x 10²
1.00 x 10⁻³
1.0 x 10³
1.000 x 10³
Happy Coding 😊