set decimals in numpy savetxt in python

To set the decimal precision in numpy's savetxt function, you can make use of the fmt parameter. The fmt parameter allows you to specify the format of the output data, including the decimal precision.

Here's an example of how you can use the fmt parameter to set the decimal precision in savetxt:

main.py
import numpy as np

# Creating an example array
arr = np.array([1.23456789, 2.34567891, 3.45678912])

# Saving the array to a file with decimal precision set to 3
np.savetxt('output.txt', arr, fmt='%.3f')
205 chars
8 lines

In the above example, the fmt='%.3f' sets the decimal precision to 3, which means the values will be saved with 3 digits after the decimal point.

You can adjust the value in the fmt parameter to control the desired decimal precision.

related categories

gistlibby LogSnag