rounding-up numbers within a tuple

后端 未结 3 1265
囚心锁ツ
囚心锁ツ 2021-01-19 01:53

Is there anyway I could round-up numbers within a tuple to two decimal points, from this:

(\'string 1\', 1234.55555, 5.66666, \'string2\')

3条回答
  •  不知归路
    2021-01-19 02:24

    List comprehension solution:

    t = ('string 1', 1234.55555, 5.66666, 'string2')
    solution = tuple([round(x,2) if isinstance(x, float) else x for x in t])
    

提交回复
热议问题