在Python中将浮点列表四舍五入为整数

问题描述:

我有一个数字列表,在继续使用该列表之前,需要将其四舍五入为整数.来源列表示例:

I have a list of numbers which I need to round into integers before I continue using the list. Example source list:

[25.0, 193.0, 281.75, 87.5, 80.5, 449.75, 306.25, 281.75, 87.5, 675.5,986.125, 306.25, 281.75]

要保存所有数字都四舍五入为整数的列表,我该怎么办?

What would I do to save this list with all of the numbers rounded to an integer?

只需使用 round 函数用于所有具有列表理解的列表成员:

Simply use round function for all list members with list comprehension :

myList = [round(x) for x in myList]

myList # [25, 193, 282, 88, 80, 450, 306, 282, 88, 676, 986, 306, 282]

如果您要 round 并带有特定的n前缀,请使用 round(x,n) :

If you want round with certain presicion n use round(x,n):