How to round each item in a list of floats to 2 decimal places?

前端 未结 6 1977
有刺的猬
有刺的猬 2021-01-31 07:20

I have a list which consists of float values but they\'re too detailed to proceed. I know we can shorten them by using the (\"%.f\" % variable) operator, like:

6条回答
  •  隐瞒了意图╮
    2021-01-31 08:02

    Another option which doesn't require numpy is:

    precision = 2  
    myRoundedList = [int(elem*(10**precision)+delta)/(10.0**precision) for elem in myList]
    
    # delta=0 for floor
    # delta = 0.5 for round
    # delta = 1 for ceil
    

提交回复
热议问题