Python allows a lot of control over formatting of output. But here we will just look at controlling decimal places of output.

There are some different ways. Here is perhaps the most common (because it is most similar to other languages).

The number use is represented in the print function as %x.yf where x is the total number of spaces to use (if defined this will add padding to short numbers to align numbers on different lines, for example), and y is the number of decimal places. f informs python that we are formatting a float (a decimal number). The %x.yf is used as a placeholder in the output (multiple palceholders are possible) and the values are given at the end of the print statement as below:

import math
pi = math.pi
pi_square = pi**2
print('Pi is %.3f, and Pi squared is %.3f' %(pi,pi_square))
OUT:
Pi is 3.142, and Pi squared is 9.870

It is also possible to round numbers before printing (or sending to a file). If taking this approach be aware that this may limit the precision of further work using these numbers:

import math
pi = math.pi
pi = round(pi,3)
print (pi)
OUT:
3.142

An applied health service researcher, currently working for the NHS and the University of Exeter. Committed to all work being performed in Free and Open Source Software (FOSS), and as much source data being made available as possible.
GitHub page: https://github.com/MichaelAllen1966
View all posts by Michael Allen

Published

One thought on “15. Python basics: decimal places in output”

## One thought on “15. Python basics: decimal places in output”