在Python中将浮点列表四舍五入为整数 [英] Rounding a list of floats into integers in Python

查看:2305
本文介绍了在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):

这篇关于在Python中将浮点列表四舍五入为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆