Last active
July 1, 2022 14:59
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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