Humanize Package in Python

23 May, 2020
Humanize is a package in python which contains various humanization utilities like turning a number into a human-readable size or throughput or number. In this article, we will discuss how to install this package and what are the different utilities present in this package.
Installation: To install this package, we will use pip a command. Python pip is the package manager for Python packages. pip comes pre-installed on 3.4 or older versions of Python, pip commands are used in the command prompt. The following command is used to install the package:
pip install humanize
Usage: This package offers various utilities which can be used on the numbers to make the numbers easily readable for the humans. The utilities of the package are:
File Size Utility: This utility can convert large integers of file sizes to human-readable form. The default unit of the size it accepts is bytes. For example:# Program to demonstrate the# File Size Utilityimport humanize  size = humanize.naturalsize(1024000)Output:1.0 MB
Scientific Notation: This utility is used to add scientific notation to the program. This utility also gives an option to add precision to the number. Precision here means the number of digits needed in the number. For example:# Program to demonstrate the# scientific notation utilityimport humanize  # Scientific notation using # integer without precisiongfg = humanize.scientific(2000)print('Without Precision: '+gfg)  # Scientific notation using # integer with precisiongfg = humanize.scientific(2**10, precision = 5)print('With Precision: '+gfg)Output:Without Precision: 2.00 x 103
With Precision: 1.02400 x 103
Floating Point to Fractions: This utility is used to convert a floating-point to fractions. For example:# Program to demonstrate the# floating point to fraction # utilityimport humanize  gfg = humanize.fractional(0.5269)print(gfg)Output:333/632
Date & Time Utility: Many a times, we encounter few scenarios where the date or time is returned in the form of numbers. This utility is used to convert the date into a human understandable format. For example:# Program to demonstrate the# date time utilityimport humanizeimport datetime as dt  # Converting the date represented# as a numbergfg = humanize.naturaldate(dt.date(2020, 5, 3))print(gfg)  # Converting seconds to a # better representationgfg = humanize.naturaldelta(dt.timedelta(seconds = 900))print(gfg)Output:May 03 2020
15 minutes
Integer Utility: This utility is used to make integer values more presentable. For Example:# Python program to demonstrate # the integer utilityimport humanize  # Adding commas to integer valuesgfg = humanize.intcomma(14523689)print(gfg)  # Converts the integer to # long and short scalesgfg = humanize.intword(1562345640)print(gfg)  # Converts numbers (0-9) to their # english formatgfg = humanize.apnumber(5)print(gfg)Output:14, 523, 689
1.6 billion
five

File Size Utility: This utility can convert large integers of file sizes to human-readable form. The default unit of the size it accepts is bytes. For example:# Program to demonstrate the# File Size Utilityimport humanize  size = humanize.naturalsize(1024000)Output:1.0 MB

# Program to demonstrate the# File Size Utilityimport humanize  size = humanize.naturalsize(1024000)
Output:
1.0 MB

Scientific Notation: This utility is used to add scientific notation to the program. This utility also gives an option to add precision to the number. Precision here means the number of digits needed in the number. For example:# Program to demonstrate the# scientific notation utilityimport humanize  # Scientific notation using # integer without precisiongfg = humanize.scientific(2000)print('Without Precision: '+gfg)  # Scientific notation using # integer with precisiongfg = humanize.scientific(2**10, precision = 5)print('With Precision: '+gfg)Output:Without Precision: 2.00 x 103
With Precision: 1.02400 x 103

# Program to demonstrate the# scientific notation utilityimport humanize  # Scientific notation using # integer without precisiongfg = humanize.scientific(2000)print('Without Precision: '+gfg)  # Scientific notation using # integer with precisiongfg = humanize.scientific(2**10, precision = 5)print('With Precision: '+gfg)
Output:
Without Precision: 2.00 x 103
With Precision: 1.02400 x 103

Floating Point to Fractions: This utility is used to convert a floating-point to fractions. For example:# Program to demonstrate the# floating point to fraction # utilityimport humanize  gfg = humanize.fractional(0.5269)print(gfg)Output:333/632

# Program to demonstrate the# floating point to fraction # utilityimport humanize  gfg = humanize.fractional(0.5269)print(gfg)
Output:
333/632

Date & Time Utility: Many a times, we encounter few scenarios where the date or time is returned in the form of numbers. This utility is used to convert the date into a human understandable format. For example:# Program to demonstrate the# date time utilityimport humanizeimport datetime as dt  # Converting the date represented# as a numbergfg = humanize.naturaldate(dt.date(2020, 5, 3))print(gfg)  # Converting seconds to a # better representationgfg = humanize.naturaldelta(dt.timedelta(seconds = 900))print(gfg)Output:May 03 2020
15 minutes

# Program to demonstrate the# date time utilityimport humanizeimport datetime as dt  # Converting the date represented# as a numbergfg = humanize.naturaldate(dt.date(2020, 5, 3))print(gfg)  # Converting seconds to a # better representationgfg = humanize.naturaldelta(dt.timedelta(seconds = 900))print(gfg)
Output:
May 03 2020
15 minutes

Integer Utility: This utility is used to make integer values more presentable. For Example:# Python program to demonstrate # the integer utilityimport humanize  # Adding commas to integer valuesgfg = humanize.intcomma(14523689)print(gfg)  # Converts the integer to # long and short scalesgfg = humanize.intword(1562345640)print(gfg)  # Converts numbers (0-9) to their # english formatgfg = humanize.apnumber(5)print(gfg)Output:14, 523, 689
1.6 billion
five

# Python program to demonstrate # the integer utilityimport humanize  # Adding commas to integer valuesgfg = humanize.intcomma(14523689)print(gfg)  # Converts the integer to # long and short scalesgfg = humanize.intword(1562345640)print(gfg)  # Converts numbers (0-9) to their # english formatgfg = humanize.apnumber(5)print(gfg)
Output:
14, 523, 689
1.6 billion
five

References: https://pypi.org/project/humanize/
python-modules
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.