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
How to round by a total digits(including integer part) instead of a fixed digit in python?
Question
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
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow