简体   繁体   中英

How to remove the decimal point from the result of an array

from array import array
scores = array('d')
scores.append(90)
scores.append(91)
print(scores)
print(scores[1])

How do we remove the decimal point from the result of an array?

scores = array('d') initializes an array of doubles (as denoted by 'd' ) rather than integers. If you want to hold an array of values without decimals construct an array this way you could write array('i') .

You might also want to take a look at other data types that may fit your needs .

If you just want to print the values without the decimal you can cast them into integers when printing as such: print(int(scores[1]))

You can string format to 0 decimal points print("{0:.0f}".format(scores[1])) .

So the value of the element '90' you stored while appending in the array is of data type float

You can eliminate decimal by converting the value to float while printing as follows

print(int(scores[1]))

Also you can use round() for eg round(arr[i],1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM