from math import isinf
def float2str(value):
if isinf(value):
raise ValueError("value not finite")
return str(value) if isinstance(value, int) else f'{value:.309g}'
You’re definitely going to run into rounding errors though.
But as it also states <number>'s are represented as “float, SVGNumber or a SVGAnimatedNumber” in SVG DOM [1], so floating point errors may still occur.
So I think that @MRAB’s first answer is the easiest and best solution for this. [2]
SVGNumber is pretty much just another name for float, the same goes for SVGAnimatedNumber↩︎
Tho it’s not converted back into a number but at that point, it shouldn’t be necessary to have it as an int or float anymore just put it in ↩︎