Decimal vs Float Python
The number system that we use in everyday life is a base-10 number system, also known as a decimal number system. We use ten unique numerals in various combinations to represent all numbers. Binary, on the other hand, is a base-2 number system and only uses two unique numerals: generally 0 and 1. When numbers are stored in our computers, they are stored in this binary format.
However, just as there are numbers we can’t represent with a finite number of decimal numerals (e.g. ⅓), there are also numbers we can’t represent in binary. For example, the number 0.1 has no finite binary representation.
Floats in Python
So what happens when we write 0.1 in Python? Let's take a look:
print(f"{0.1:.20f}") # 0.10000000000000000555
# Warn: Jupyternotebook truncates the undesirable part in floats in their outputs.For those of you not familiar with the syntax above, the :.20f is a way of telling Python we want 20 digits after the decimal point for this float.
Creating Decimal objects
Before we dive into Decimal objects, let's look at how to define them. There are actually several ways to do this.
Using an integer
The first way to create a Decimal object us to use an integer. In this case, we simply pass the integer in as an argument to the Decimal constructor:
import decimalx = decimal.Decimal(34) # 34
We can now use x just like any other number, and we'll get a Decimal object back when performing mathematical operations:
x = decimal.Decimal(34)
print(x + 7) # 41
print(type(x + 7)) # <class 'decimal.Decimal'>
print(x // 7) # 4
print(type(x // 7)) # <class 'decimal.Decimal'>Using a string
Perhaps a little surprisingly, one of the easiest ways to make a Decimal object with fractional components is using a string. We just need to pass a string representation of the number to Decimal and it'll take care of the rest:
x = decimal.Decimal("0.1")
print(x) # 0.1
print(f"{x:.20f}") # 0.10000000000000000000As we can see, printing x to 20 decimal places here gives us 19 zeroes: we don't end up with some random 5s at the end like we did when using float.
Get Young Hwang’s stories in your inbox
Join Medium for free to get updates from this writer.
If you need a precise decimal representation of a number, using strings to create your Decimal objects is a very simple way to achieve this.
Using a float (not recommended)
It’s also possible to create a Decimal object from a float, but I'd generally advise against doing this. The example below should make it clear as to why:
x = decimal.Decimal(0.1)
print(x) # 0.1000000000000000055511151231257827021181583404541015625Converting straight from a float causes our Decimal object to inherit all of the imprecision that we were trying to avoid in the first place. There may be cases where you want to preserve this imprecision for some reason, but most of the time, that's not the case.
Using a tuple ( not common )
Perhaps the most complicated method of creating a Decimal object is using a tuple, and this method gives us some insight into how Decimal objects work under the hood.
The tuple we provide to Decimal has three parts. The first element in the tuple is either the number 0 or 1, and this represents the sign of the number (whether it's positive or negative). A zero in this first position indicates a positive number, while a one represents a negative number.
The second item in the tuple is another tuple, and this contains all of the digits in the resulting number. For example, the number 1.3042 has the digits (1, 3, 0, 4, 2).
The third and final item in the tuple is an exponent. This tells the Decimal object how many places we need to shift the digits around the decimal point. An exponent of -3 is going to cause the digits to shift 3 spaces to the right, giving us 13.042, while an exponent of 2 is going to give us 1304200.
A complete example for the number -13.042 looks like this:
x = decimal.Decimal((1, (1, 3, 0, 4, 2), -3))
print(x) # -13.042A really important thing to note here is that we do need the brackets around the outer tuple, as Decimal expects this tuple as a single argument. Removing the outer brackets will cause an error.
While relatively complicated, the tuple syntax is best suited to creating Decimal objects programmatically.
The disadvantage of using decimal is that it is down to performance. decimal operations are likely to be around 3 times slower than operations using floats, so if you're dealing with a performance sensitive part of your application, where absolute precision isn't important, you might want to stay clear of decimal.






