From 2d41f7115f32544c995687c96963b9ddc36c7f12 Mon Sep 17 00:00:00 2001 From: xi Date: Thu, 22 Mar 2007 16:13:32 +0000 Subject: Improve output of float values. Fix #49. git-svn-id: http://svn.pyyaml.org/pyyaml/trunk@248 18f92427-320e-0410-9341-c67f048884a3 --- lib/yaml/representer.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/yaml/representer.py b/lib/yaml/representer.py index 072ed9b..1f4fe59 100644 --- a/lib/yaml/representer.py +++ b/lib/yaml/representer.py @@ -197,7 +197,16 @@ class SafeRepresenter(BaseRepresenter): elif data == -self.inf_value: value = u'-.inf' else: - value = unicode(repr(data)) + value = unicode(repr(data)).lower() + # Note that in some cases `repr(data)` represents a float number + # without the decimal parts. For instance: + # >>> repr(1e17) + # '1e17' + # Unfortunately, this is not a valid float representation according + # to the definition of the `!!float` tag. We fix this by adding + # '.0' before the 'e' symbol. + if u'.' not in value and u'e' in value: + value = value.replace(u'e', u'.0e', 1) return self.represent_scalar(u'tag:yaml.org,2002:float', value) def represent_list(self, data): -- cgit v1.2.1