Skip to content

Encode decimal_string values in fixed-point notation - #2002

Open
zacchua-stripe wants to merge 1 commit into
masterfrom
zacchua/decimal-string-fixed-point
Open

zacchua-stripe wants to merge 1 commit into
masterfrom
zacchua/decimal-string-fixed-point

Conversation

@zacchua-stripe

@zacchua-stripe zacchua-stripe commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Why?

V2 decimal_string fields are sent as strings, and _encode_decimal converts a Decimal or float to that string with str().

str() 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 only accepts decimal strings in fixed-point notation, so it rejects these values. This affects the Decimal type the SDK asks users to pass, not just floats.

What?

  • _encode_decimal formats finite Decimal values with format(value, "f"), and converts finite floats through Decimal(repr(value)) first so they keep their shortest round-trip digits (0.1 stays "0.1")
  • values that str() already writes in fixed-point notation, non-finite values, and ints are unchanged
  • this covers decimal_string request params, Decimal values in JSON request bodies, and StripeObject JSON output, which all go through _encode_decimal
  • add tests for small and large Decimal and float values, unchanged values, lists, and a JSON request body

See Also

https://go/j/RUN_DEVSDK-3739

Configuration

  • skip-changefile: This PR is not a user-facing change, so there's no changefile.

_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 <noreply@anthropic.com>
Committed-By-Agent: claude
@zacchua-stripe
zacchua-stripe marked this pull request as ready for review November 23, 2026 20:32
@zacchua-stripe
zacchua-stripe requested a review from a team as a code owner November 23, 2026 20:32
@zacchua-stripe
zacchua-stripe requested review from jar-stripe and mark-kogan-stripe and removed request for a team November 23, 2026 20:32

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant