1

I have numpy arrays as follows.

x = array([0.1])
y = array([0.2])
z= array([-0.05])

I want to get the value of them as a number. Therefore, I did the follwoing.

print(x.tolist()[0])
print(y.tolist()[0])
print(z.tolist()[0])

However, it gave me the following results which is incorrect.

0.09999999999999996
0.19999999999999998
-0.049999999999999975

I am wondering if there is a way to get 0.1, 0.2, -0.05 respectively.

I am happy to provide more details if needed.

EmJ
  • 4,398
  • 9
  • 44
  • 105
  • @Austin I used it. However, it still gives me `0.09999999999999996 0.19999999999999998 -0.049999999999999975` – EmJ Feb 12 '19 at 10:03

2 Answers2

2

You can use this:

float(x[0])
float(y[0])
float(z[0])

and for precision, you can use

round

renny
  • 590
  • 4
  • 23
1

You can use like this

print(round(x.tolist()[0], 2))
print(round(y.tolist()[0], 2))
print(round(z.tolist()[0], 2))
Manzurul Hoque Rumi
  • 2,911
  • 4
  • 20
  • 43