Skip to content
Advertisement

Is there an python function or extension that is is similar to Matlab’s format short?

The command format short in Matlab makes all the print outs in the command window be “Short, fixed-decimal format with 4 digits after the decimal point.”

I know there is np.round, but I would like to have this functionality that Matlab offers in python so I dont have to write round every time. This in order to get a better overview of arrays/dataframes when they are printed.

I am interested in automatic rounding of numbers/floats printed in the terminal without using np.round

Ideally I would like also to be able to choose the number of digits (4).

Thanks

Advertisement

Answer

You can use numpy.set_printoptions, from the documentation:

np.set_printoptions(precision=4)
np.array([1.123456789])
[1.1235]
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement