From 00405c15d7ec80438dbeb019e229d0bce2f96146 Mon Sep 17 00:00:00 2001 From: Zachary Chua Date: Mon, 23 Nov 2026 11:57:30 -0800 Subject: [PATCH] Encode decimal_string values in fixed-point notation _encode_decimal used str(), which switches to exponent notation for small and large values: str(0.00001) is "1e-05", str(1e16) is "1e+16", and str(Decimal("0.0000001")) is "1E-7". The V2 API rejects decimal strings in exponent notation. Format finite Decimals with format(dec, "f"), and convert finite floats through Decimal(repr(value)) first so they keep their shortest round-trip digits. Values that str() already wrote in fixed-point, non-finite values, and ints are unchanged. Co-Authored-By: Claude Opus 5.5 Committed-By-Agent: claude --- ...cchua_decimal-string-fixed-point.change.md | 7 +++ stripe/_encode.py | 8 +++ tests/test_decimal_string.py | 60 ++++++++++++++++++- 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 .hark/changes/2026-11-23_zacchua_decimal-string-fixed-point.change.md diff --git a/.hark/changes/2026-11-23_zacchua_decimal-string-fixed-point.change.md b/.hark/changes/2026-11-23_zacchua_decimal-string-fixed-point.change.md new file mode 100644 index 000000000..b7f441c34 --- /dev/null +++ b/.hark/changes/2026-11-23_zacchua_decimal-string-fixed-point.change.md @@ -0,0 +1,7 @@ +--- +title: Encode `decimal_string` values in fixed-point notation +semver_level: patch +--- + +- `Decimal` and `float` values for `decimal_string` fields are now sent in fixed-point notation. Previously, small and large values used exponent notation, which V2 endpoints reject: `0.00001` was sent as `"1e-05"`, `1e16` as `"1e+16"`, and `Decimal("0.0000001")` as `"1E-7"`. Now they are sent as `"0.00001"`, `"10000000000000000"`, and `"0.0000001"`. +- `Decimal` values in `StripeObject` JSON output use fixed-point notation as well. diff --git a/stripe/_encode.py b/stripe/_encode.py index 7c402cf52..96daaf9f1 100644 --- a/stripe/_encode.py +++ b/stripe/_encode.py @@ -1,5 +1,6 @@ import calendar import datetime +import math import time from collections import OrderedDict from decimal import Decimal @@ -16,6 +17,13 @@ def _encode_datetime(dttime: datetime.datetime): def _encode_decimal(dec) -> str: + # str() uses exponent notation for small and large values (str(0.00001) is + # "1e-05" and str(Decimal("0.0000001")) is "1E-7"), which the API rejects. + if isinstance(dec, float) and math.isfinite(dec): + # repr() keeps the float's shortest round-trip digits, so 0.1 stays "0.1" + dec = Decimal(repr(dec)) + if isinstance(dec, Decimal) and dec.is_finite(): + return format(dec, "f") return str(dec) diff --git a/tests/test_decimal_string.py b/tests/test_decimal_string.py index 8a750a905..0b510dbcb 100644 --- a/tests/test_decimal_string.py +++ b/tests/test_decimal_string.py @@ -7,9 +7,14 @@ - Response hydration: JSON string → Decimal """ +import json from decimal import Decimal -from stripe._encode import _coerce_decimal_string, _coerce_v2_params +from stripe._encode import ( + _coerce_decimal_string, + _coerce_v2_params, + _make_suitable_for_json, +) from stripe._stripe_object import StripeObject @@ -88,6 +93,59 @@ def test_negative(self): assert _coerce_decimal_string("-1.5", encode=False) == Decimal("-1.5") +class TestDecimalStringFixedPoint: + """The API rejects exponent notation, so values are encoded in fixed-point.""" + + def test_small_decimal(self): + assert ( + _coerce_decimal_string(Decimal("0.0000001"), encode=True) + == "0.0000001" + ) + assert ( + _coerce_decimal_string(Decimal("1E-7"), encode=True) == "0.0000001" + ) + assert ( + _coerce_decimal_string(Decimal("-2.5E-7"), encode=True) + == "-0.00000025" + ) + + def test_decimal_with_positive_exponent(self): + assert _coerce_decimal_string(Decimal("1E+3"), encode=True) == "1000" + + def test_small_float(self): + assert _coerce_decimal_string(0.00001, encode=True) == "0.00001" + assert _coerce_decimal_string(-2.5e-7, encode=True) == "-0.00000025" + + def test_large_float(self): + assert _coerce_decimal_string(1e16, encode=True) == "10000000000000000" + assert ( + _coerce_decimal_string(1.5e19, encode=True) + == "15000000000000000000" + ) + + def test_float_keeps_shortest_digits(self): + assert _coerce_decimal_string(0.1, encode=True) == "0.1" + assert ( + _coerce_decimal_string(1e15, encode=True) == "1000000000000000.0" + ) + + def test_non_finite_unchanged(self): + assert _coerce_decimal_string(float("nan"), encode=True) == "nan" + assert _coerce_decimal_string(float("inf"), encode=True) == "inf" + assert _coerce_decimal_string(Decimal("NaN"), encode=True) == "NaN" + + def test_list(self): + assert _coerce_decimal_string( + [Decimal("1E-7"), 1e16], encode=True + ) == ["0.0000001", "10000000000000000"] + + def test_json_request_body(self): + body = json.dumps( + {"price": Decimal("1E-7")}, default=_make_suitable_for_json + ) + assert body == '{"price": "0.0000001"}' + + class TestCoerceV2ParamsDecimalString: """Tests for outbound request coercion (Decimal → str)."""