Reputation: 66320
jsons = json.loads(request.data)
jsons -->
dict: {u'json_event': {u'timestamp': 1408878136.318921}}
and
json_event = jsons['json_event']
json_event -->
dict: {u'timestamp': 1408878136.318921}
However when I do json_event['timestamp']
I only get two decimal places precision:
float: 1408878136.32
Is there a way to keep the precision?
Update:
I don't think this is a representation problem.
event, is_created = Event.create_or_update(json_event['event_id'],
timestamp=json_event['timestamp'])
class Event(ndb.Model):
...
timestamp = ndb.FloatProperty(required=True)
event.timestamp --> 1408878136.32
Upvotes: 0
Views: 771
Reputation: 76276
When you—or whatever tool you use to print the numbers—uses standard conversion to string, only 12 significant digits get printed:
>>> str(1408878136.318921)
'1408878136.32'
But when you use the repr
builtin, enough significant digits get printed to ensure identical value would be read back by python parser:
>>> repr(1408878136.318921)
'1408878136.318921'
So just wrap whatever you are printing in a manual repr()
call.
This is just representational issue. Obviously the JSON printer uses some logic (may be via repr
or may not) to print enough digits to read back the same value. But the tool you are using to print them is not.
Note that the logic is pretty complex, because binary fractional numbers don't correspond exactly to decimal fractional numbers. 0.3
has periodic representation in binary and thus if you read 0.3
, the actual number stored will have slightly different value. And a closest decimal representation is different. So the logic has to consider how much rounding it can apply to still read back the correct value.
Upvotes: 2