summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthon van der Neut <anthon@mnt.org>2018-08-30 21:00:09 +0200
committerAnthon van der Neut <anthon@mnt.org>2018-08-30 21:00:09 +0200
commit209ce7fbd33c12263980c6c1468d36d0ffbf8ea7 (patch)
treefd887c93272fa43143cc16bbe449b7244bcc5c37
parent95e1aa1d1226e148ed9b43ed714635c32d8d455b (diff)
downloadruamel.yaml-209ce7fbd33c12263980c6c1468d36d0ffbf8ea7.tar.gz
fix issue #229 enable round-trip of floats without number before the dot
[ .5, -.5, +.5 ] should work *When this change indeed resolves your problem, please **Close** this issue*. *(You can do so using the WorkFlow pull-down (close to the top right of this page))*
-rw-r--r--_test/test_float.py4
-rw-r--r--representer.py4
-rw-r--r--resolver.py2
3 files changed, 7 insertions, 3 deletions
diff --git a/_test/test_float.py b/_test/test_float.py
index 2012b15..1f104f3 100644
--- a/_test/test_float.py
+++ b/_test/test_float.py
@@ -22,6 +22,8 @@ class TestFloat:
- -42.
- +42.
- .5
+ - +.5
+ - -.5
""")
print(data)
assert 0.999 < data[0] < 1.001
@@ -34,6 +36,8 @@ class TestFloat:
assert 41.999 < -data[7] < 42.001
assert 41.999 < data[8] < 42.001
assert .49 < data[9] < .51
+ assert .49 < data[10] < .51
+ assert -.51 < data[11] < -.49
def test_round_trip_zeros_0(self):
data = round_trip("""\
diff --git a/representer.py b/representer.py
index 6bd5651..dba7901 100644
--- a/representer.py
+++ b/representer.py
@@ -788,13 +788,13 @@ class RoundTripRepresenter(SafeRepresenter):
elif data._exp is None:
# no exponent, "normal" dot
prec = data._prec
- if prec < 1:
- prec = 1
ms = data._m_sign if data._m_sign else ""
# -1 for the dot
value = u'{}{:0{}.{}f}'.format(
ms, abs(data), data._width - len(ms), data._width - prec - 1
)
+ if prec == 0 or (prec == 1 and ms != ""):
+ value = value.replace(u'0.', u'.')
while len(value) < data._width:
value += u'0'
else:
diff --git a/resolver.py b/resolver.py
index bada11e..2641a54 100644
--- a/resolver.py
+++ b/resolver.py
@@ -38,7 +38,7 @@ implicit_resolvers = [
RegExp(u'''^(?:
[-+]?(?:[0-9][0-9_]*)\\.[0-9_]*(?:[eE][-+]?[0-9]+)?
|[-+]?(?:[0-9][0-9_]*)(?:[eE][-+]?[0-9]+)
- |\\.[0-9_]+(?:[eE][-+][0-9]+)?
+ |[-+]?\\.[0-9_]+(?:[eE][-+][0-9]+)?
|[-+]?\\.(?:inf|Inf|INF)
|\\.(?:nan|NaN|NAN))$''', re.X),
list(u'-+0123456789.')),