Rounding to significant figures in Python

A Python code snippet

Matt Gosden
Jun 9, 2022

I don’t know why there is no rounding function in the Python standard library that allows rounding to a certain number of significant figures. I come across the need to do this quite a lot.

The attached little function does the job. Perhaps something that does this job should make it into the math standard library in the future?

Photo by Hitesh Choudhary on Unsplash

--

--