How to print floating points with *limited* precision? ideally with %g semantics


#1

I’m wondering if there is any ergonomic way to print floating point numbers with something akin to “%.3g”, which prints the numbers with three significant decimal places. A natural syntax for this would be “{:.2}”, but that corresponds to “%.3f”, which is a fixed-point syntax that may give extra digits of precision, or may have no digits of precision (if the number is less than 0.01). Similarly, “{:.2e}” may give digits with value zero at the end.

Does anyone have a way to format similarly to %g, which gives a representation with few characters and limits the precision of the output?

EDIT: After writing this, I came across this thread which discusses the same issue, and doesn’t seem to have a real answer.