Formatting Floating Points Before Decimal Separator in Python

Formatting Floating Point In Python

Machines & humans, though both work together in harmony, the approach each takes to comprehend things are poles apart. The comprehensible capability of a human is influenced by how the data is being presented whilst a machine has no such vulnerability. For instance, without the comma separators a human might find it difficult to identify whether the number is a million or billion at a glance, but the same couldn’t be told true for the machines.

In this article, we shall explore the ways in which the floating point in Python could be formatted before the decimal separator.

  • Need for Formatting before Decimal Separator
  • How to Format Floating Point before Decimal Separator?

Understanding the Need for Decimal Formatting

At times, there might be a need to display the results in a nice and presentable manner. Thereby the indentation of each result matters the most. For the results to be presentable, they should not all have the same indentation. Have a look at the below list of numbers for instance.

ar1 = 5.6
ar2 = 105
ar3 = 0.059
print (ar1,"\n", ar2,"\n", ar3)
Misaligned Number Printing
The Issue: Misaligned Number Printing

It can be seen in the above image that the decimal separator of all three values printed are not aligned. This can leave a dent in the way in which the data is presented, making the reader put some effort into perceiving which among the digits fall before & after the decimal separator.

So, there needs to be a means by which the digits before the decimal separator can be formatted in order to make the results display in a manner palatable to the eyes.


How to Format Floating Point before Decimal Separator?

Before one gets on with formatting the digits before the decimal separator, it is high time for an introduction to an exclusive operator – the ‘%’ operator. This is the go-to operator when the coder likes to format the digits of the resulting number. The operator functions based on the inputs from three constructs. Let us have a look at those three constructs from the syntax given below,

% fw.p conv

Where,

  • fw – The width of the field which specifies the total count of characters in the displayed value
  • p – Used to specify the number of digits that is to follow the decimal point
  • conv – Used to specify the format type in which the result is to be displayed

With this know-how let us now try to turn the results of the numbers printed earlier into something which is pretty & readable. One can set things off by tweaking the inhabitants of the print( ) function using the % operator as shown below.

print ("%6.2f" % ar1,"\n","%6.2f" % ar2,"\n","%6.2f" % ar3)
All Values Before After Deicmal Separator Aligned
The Result: Aligned Decimal Values

Let us break down what has happened in the above code. Firstly we tell Python that there need to be six characters in the printed result by placing 6 after the % operator (%6.2f). This explains why the first value ‘5.60’ & the last value ‘0.06’ are offset by 2 blank characters to align them along with the same position as the decimal separator of ‘105.00’.

But the original value of ar2 was ‘105’ & not ‘105.00’. So how did this come by? This can be explained by the number two following the dot ‘.’ in the % operator (%6.2f). The letter f that follows ‘2’ in the % operator indicates that the end result should be in the form of floats.

So, there you have it! Putting the % operator in use for the formatting of floating point before the decimal separator in Python.


Summary

Now that we have reached the end of this article, hope it has elaborated on the formatting of the floating point in Python before the decimal separator. Here’s another article that details how to use the all powerful einsum_path function from the Numpy library in Python. There are numerous other enjoyable and equally informative articles in AskPython that might be of great help to those who are looking to level up in Python. Audere est facere!


References