Home > Software engineering >  How do I get rid of unnecessary dot symbol for float ending with 0
How do I get rid of unnecessary dot symbol for float ending with 0

Time:04-22

For example I have an array in Python that looks like this

x = np.array([0, 1, 1.5, 2])

But when I do a print(x), it shows this instead

x = [0.  1.  1.5 2. ]

How do I make it so that it prints x = [0 1 1.5 2] without the "." symbol for numbers that don't have any other decimals?

CodePudding user response:

Since you have a NumPy array of floats you get the dots when you print. To just print them without dots you have to convert the numbers in your list to integers if they can be represented as an integer.

print([int(y) if y.is_integer() else y for y in x])
  • Related