Skip to content

Commit f6e9618

Browse files
Add tests to cover serializing scientific notation
1 parent 6482d73 commit f6e9618

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

python-packages/smithy-python/smithy_python/utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,11 @@ def serialize_float(given: float | Decimal) -> str:
166166
if isinf(given):
167167
return "-Infinity" if given < 0 else "Infinity"
168168

169+
if isinstance(given, Decimal):
170+
given = given.normalize()
171+
169172
result = str(given)
170-
if "." not in result:
173+
if result.isnumeric():
171174
result += ".0"
172175
return result
173176

python-packages/smithy-python/tests/unit/test_utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,27 @@ def test_strict_parse_float_raises(given: str) -> None:
166166
(1, "1.0"),
167167
(1.0, "1.0"),
168168
(1.1, "1.1"),
169+
# It's not particularly important whether the result of this is "1.1e3" or
170+
# "1100.0" since both are valid representations. This is how float behaves
171+
# by default in python though, and there's no reason to do extra work to
172+
# change it.
173+
(1.1e3, "1100.0"),
174+
(1e1, "10.0"),
175+
(32.100, "32.1"),
176+
(0.321000e+2, "32.1"),
177+
# It's at about this point that floats start using scientific notation.
178+
(1e16, "1e+16"),
169179
(float("NaN"), "NaN"),
170180
(float("Infinity"), "Infinity"),
171181
(float("-Infinity"), "-Infinity"),
172182
(Decimal("1"), "1.0"),
173183
(Decimal("1.0"), "1.0"),
174184
(Decimal("1.1"), "1.1"),
185+
(Decimal("1.1e3"), "1.1E+3"),
186+
(Decimal("1e1"), "1E+1"),
187+
(Decimal("32.100"), "32.1"),
188+
(Decimal("0.321000e+2"), "32.1"),
189+
(Decimal("1e16"), "1E+16"),
175190
(Decimal("NaN"), "NaN"),
176191
(Decimal("Infinity"), "Infinity"),
177192
(Decimal("-Infinity"), "-Infinity"),

0 commit comments

Comments
 (0)