2

I have a float let's say

x = 2.00

I want to send that as json

message = { 'x': 2.00 } 

But when I do

print(message)

I see that python has dropped the last decimal place. How can I keep the float at two decimal places? I understand that 2.00 and 2.0 are not different but it's a requirement that I send that exact number (two decimal places included) (I have tried the Decimal class, it still acts the same and I need to send it as a float not a string). Thanks in advance.

Morpheus.47
  • 125
  • 1
  • 9
  • This cannot be done in `json` as there is no requirement on the formatting of floats. This would have to be done after the `json` has been decoded again, by formatting as a string. See: https://stackoverflow.com/questions/8885663/how-to-format-a-floating-number-to-fixed-width-in-python – quamrana Jan 02 '20 at 14:10

3 Answers3

0

You would need to use a specialized library for serializing floats and retaining precision. Protocol Buffers is one such library (protocol)you can define your own JSON encoder class: https://developers.google.com/protocol-buffers/docs/proto3.

kederrac
  • 15,932
  • 5
  • 29
  • 52
Avi Kaminetzky
  • 1,308
  • 1
  • 19
  • 41
0

you can define your own JSON encoder class:

import json

message = {'x': 2.00}

class MyEncoder(json.JSONEncoder):
    def encode(self, obj):

        if isinstance(obj, dict):
            result = '{'
            for key, value in obj.items():
                if isinstance(value, float):
                    encoded_value = format(value, '.2f')
                else:
                    encoded_value = json.JSONEncoder.encode(self, value)

                result += f'"{key}": {encoded_value}, '

            result = result[:-2] + '}'
            return result
        return json.JSONEncoder.encode(self, obj)

print(json.dumps(message, cls=MyEncoder))

output:

{"x": 2.00}
kederrac
  • 15,932
  • 5
  • 29
  • 52
-1

Here's an example:

a=2.00
print ("{0:.2f}".format(a))
Henridv
  • 684
  • 14
  • 23
Tej
  • 11
  • 1