1

I wonder if there is any easy way to round a double number to a total digits in python? For example, I want 3 digits in total, so I want to 1.523 to be 1.52 , and 23.45 to be 23.5, and 108,9 to be 109

thanks a lot

4

1 回答 1

3
sround = lambda x,d: round(x,d - int(math.ceil(math.log10(abs(x)))))

sround(1.2345, 3) # 1.23
sround(12345.67, 3) # 12300.0
sround(-.01234, 1) # -0.01
sround(199, 1) # 200.0
于 2013-04-22T16:33:51.597 回答