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.

1
  • @Austin I used it. However, it still gives me 0.09999999999999996 0.19999999999999998 -0.049999999999999975 Commented Feb 12, 2019 at 10:03

2 Answers 2

2

You can use this:

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

and for precision, you can use

round

Sign up to request clarification or add additional context in comments.

Comments

1

You can use like this

print(round(x.tolist()[0], 2))
print(round(y.tolist()[0], 2))
print(round(z.tolist()[0], 2))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.