Skip to content

Instantly share code, notes, and snippets.

@gustavofonseca
Last active July 1, 2022 14:59
Show Gist options
  • Save gustavofonseca/5b1d8c5930c0946aa092d23fb7f589a4 to your computer and use it in GitHub Desktop.
Save gustavofonseca/5b1d8c5930c0946aa092d23fb7f589a4 to your computer and use it in GitHub Desktop.
Função que codifica objetos Python em JSON fazendo com que objetos float sejam codificados com precisão de 2 casas decimais
def dumps(obj):
result = []
for part in json.JSONEncoder().iterencode(obj):
try:
tmp = round(float(part), 2)
except ValueError:
pass
else:
if "." in part:
part = '{:.2f}'.format(tmp)
result.append(part)
return ''.join(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment