Skip to content
This repository was archived by the owner on Oct 29, 2024. It is now read-only.

Commit 459eec8

Browse files
committed
chore(line_protocol): update repr value of floats to properly handle precision. Closes #488
1 parent 7c858bf commit 459eec8

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

influxdb/line_protocol.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def _escape_value(value):
113113
return str(value) + 'i'
114114

115115
if _is_float(value):
116-
return repr(value)
116+
return repr(float(value))
117117

118118
return str(value)
119119

influxdb/tests/test_line_protocol.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
from __future__ import print_function
77
from __future__ import unicode_literals
88

9-
from datetime import datetime
109
import unittest
11-
from pytz import UTC, timezone
1210

11+
from datetime import datetime
12+
from decimal import Decimal
13+
14+
from pytz import UTC, timezone
1315
from influxdb import line_protocol
1416

1517

@@ -166,3 +168,20 @@ def test_float_with_long_decimal_fraction(self):
166168
line_protocol.make_lines(data),
167169
'test float_val=1.0000000000000009\n'
168170
)
171+
172+
def test_float_with_long_decimal_fraction_as_type_decimal(self):
173+
"""Ensure precision is preserved when casting Decimal into strings."""
174+
data = {
175+
"points": [
176+
{
177+
"measurement": "test",
178+
"fields": {
179+
"float_val": Decimal(1.0000000000000009),
180+
}
181+
}
182+
]
183+
}
184+
self.assertEqual(
185+
line_protocol.make_lines(data),
186+
'test float_val=1.0000000000000009\n'
187+
)

0 commit comments

Comments
 (0)