Skip to content

Commit 8e02ea8

Browse files
authored
Merge pull request influxdata#430 from isilien/precise-floats (Thanks @isilien!)
fix for lost precision on float field values
2 parents 11c71f2 + eb049f1 commit 8e02ea8

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

influxdb/line_protocol.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,22 @@ def quote_literal(value):
7474
)
7575

7676

77+
def _is_float(value):
78+
try:
79+
float(value)
80+
except ValueError:
81+
return False
82+
return True
83+
84+
7785
def _escape_value(value):
7886
value = _get_unicode(value)
7987
if isinstance(value, text_type) and value != '':
8088
return quote_ident(value)
8189
elif isinstance(value, integer_types) and not isinstance(value, bool):
8290
return str(value) + 'i'
91+
elif _is_float(value):
92+
return repr(value)
8393
else:
8494
return str(value)
8595

influxdb/tests/test_line_protocol.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,20 @@ def test_quote_literal(self):
119119
line_protocol.quote_literal(r"""\foo ' bar " Örf"""),
120120
r"""'\\foo \' bar " Örf'"""
121121
)
122+
123+
def test_float_with_long_decimal_fraction(self):
124+
"""Ensure precision is preserved when casting floats into strings."""
125+
data = {
126+
"points": [
127+
{
128+
"measurement": "test",
129+
"fields": {
130+
"float_val": 1.0000000000000009,
131+
}
132+
}
133+
]
134+
}
135+
self.assertEqual(
136+
line_protocol.make_lines(data),
137+
'test float_val=1.0000000000000009\n'
138+
)

0 commit comments

Comments
 (0)