Skip to content

Commit 80d21de

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

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,25 @@ 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. The same applies to Decimal, though that produces the opposite
173+
# result by default.
174+
(1.1e3, "1100.0"),
175+
(1e1, "10.0"),
176+
(32.100, "32.1"),
177+
(0.321000e+2, "32.1"),
169178
(float("NaN"), "NaN"),
170179
(float("Infinity"), "Infinity"),
171180
(float("-Infinity"), "-Infinity"),
172181
(Decimal("1"), "1.0"),
173182
(Decimal("1.0"), "1.0"),
174183
(Decimal("1.1"), "1.1"),
184+
(Decimal("1.1e3"), "1.1E+3"),
185+
(Decimal("1e1"), "1E+1"),
186+
(Decimal("32.100"), "32.1"),
187+
(Decimal("0.321000e+2"), "32.1"),
175188
(Decimal("NaN"), "NaN"),
176189
(Decimal("Infinity"), "Infinity"),
177190
(Decimal("-Infinity"), "-Infinity"),

0 commit comments

Comments
 (0)