How to multiply with a Decimal in Python

avatar

Borislav Hadzhiev

Last updated: Jul 9, 2022

banner

Photo from Unsplash

Multiply with a Decimal in Python #

To multiply with a decimal:

  1. Use the Decimal() class from the decimal module to get a decimal number.
  2. Use the multiplication * operator to multiply the decimal by another number.
  3. Optionally round the result to N digits precision.
main.py
from decimal import Decimal decimal_1 = input('Enter your favorite decimal: ') print(decimal_1) # 👉️ '.4' my_int = 3 # 👇️ convert string to decimal result = Decimal(decimal_1) * my_int print(result) # 👉️ 1.2 # -------------------------------- decimal_2 = Decimal(3.14) result_2 = decimal_2 * 4 print(result_2) # 👉️ 12.56000... # 👇️ use round() to round to N digits precision print(round(result_2, 2)) # 👉️ 12.56

The first example uses the input() function to get a decimal number from user input.

The input function takes an optional prompt argument and writes it to standard output without a trailing newline.

The function then reads the line from input, converts it to a string and returns the result.

Note that the input() function always returns a string, even if the user enters a number.

You can pass the string to the Decimal() class to convert it to a decimal.

main.py
from decimal import Decimal decimal_1 = input('Enter your favorite decimal: ') print(decimal_1) # 👉️ '.4' my_int = 3 # 👇️ convert string to decimal result = Decimal(decimal_1) * my_int print(result) # 👉️ 1.2

The decimal.Decimal class constructs a new Decimal object from a value.

The value we pass to the decimal.Decimal class can be an integer, string, tuple, float, or another Decimal object.

If you call the Decimal class without passing a value, it returns 0.

If the user enters a value that is not a valid decimal number, a decimal.InvalidOperation error is raised.

You can use a try/except block if you need to handle the error.

main.py
from decimal import Decimal, InvalidOperation decimal_1 = input('Enter your favorite decimal: ') print(decimal_1) # 👉️ '.4' my_int = 3 try: result = Decimal(decimal_1) * my_int print(result) # 👉️ 1.2 except InvalidOperation: print('Value cannot be converted to decimal')

If you need to round the multiplication result to N digits precision, use the round() function.

main.py
from decimal import Decimal decimal_2 = Decimal(3.14) result_2 = decimal_2 * 4 print(result_2) # 👉️ 12.56000... # 👇️ use round() to round to N digits precision print(round(result_2, 2)) # 👉️ 12.56

The round function takes the following 2 parameters:

NameDescription
numberthe number to round to ndigits precision after the decimal
ndigitsthe number of digits after the decimal the number should have after the operation (optional)

The round function returns the number rounded to ndigits precision after the decimal point.

If ndigits is omitted, the function returns the nearest integer.

main.py
from decimal import Decimal fav_num = Decimal(3.456) result1 = round(fav_num) print(result1) # 👉️ 3 result2 = round(fav_num, 2) print(result2) # 👉️ 3.46
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.