I have a nested dict and some of the (nested) values are floating numbers.
how can I integrate a custom formatter that can specify the number of significant digits?
perhaps it would be a good idea to add an option of float formating?
def custom_formatter(x):
# Check if x is a float and format it to 2 significant digits
if isinstance(x, float):
return f'{x:.2f}'
# Recursively apply the formatter to nested dictionaries and lists
elif isinstance(x, dict):
return {k: custom_formatter(v) for k, v in x.items()}
elif isinstance(x, list):
return [custom_formatter(v) for v in x]
else:
return x
I have a nested dict and some of the (nested) values are floating numbers. how can I integrate a custom formatter that can specify the number of significant digits?
perhaps it would be a good idea to add an option of float formating?
input:
desire output: ic(my_dict)