Nuddel69
Nuddel69

Reputation: 9

How can .0 decimals be included when working with numpy floats

I have a numpy array containing mostly whole numbers and floats. The way I understand it, arrays are always stored as floats, and so the integers are stored as <number>.0. I want to work with the entries of this array as whole floats, but after some testing, it seems the whole numbers are printed as <number>. excluding the .0 decimal. I will not be printing these, so np.set_printoptions won't help me. I've also tried adding '%.1f' % which works, but isn't a possible solution as it turns my float into a string.

verticies = np.array([
    [4.5, 2],
    [0, 1],
    [-1.5, 2], 
    [1.5, 2], 
    [1.5, 1.5], 
    [2, 1.5], 
    [2, -0.5], 
    [1.5, -0.5], 
    [1.5, -2], 
    [0.5, -2], 
    [0.5, -4.5], 
    [-0.5, -4.5], 
    [-0.5, -2], 
    [-1.5, -2], 
    [-1.5, -0.5], 
    [-2, -0.5], 
    [-2, 1.5], 
    [-1.5, 1.5]
])

origin = np.array([
    [verticies[0][0]],
    [verticies[0][1]]
])

print(origin)

I would expect this to print [[4.5], [2.0]], but instead it prints [[4.5] [2. ]]

In short: how can I include the decimal 0 after 2.?

Any help would be greatly appreaciated!

Upvotes: 0

Views: 1334

Answers (1)

Mutaz-MSFT
Mutaz-MSFT

Reputation: 806

use np.set_printoptions

float_formatter = "{:.1f}".format
np.set_printoptions(formatter={'float_kind':float_formatter})
print(origin)

output

[[4.5]
[2.0]]

Upvotes: 2

Related Questions