Skip to content

Commit

Permalink
Add comment about converting to Decimal before encode
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewheard committed Feb 28, 2024
1 parent 6ab6377 commit c6157e6
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Sources/GoogleAI/JSONValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ extension JSONValue: Encodable {
case .null:
try container.encodeNil()
case let .number(numberValue):
// Convert to `Decimal` before encoding for consistent floating-point serialization across
// platforms. E.g., `Double` serializes 3.14159 as 3.1415899999999999 in some cases and
// 3.14159 in others. See
// https://forums.swift.org/t/jsonencoder-encodable-floating-point-rounding-error/41390/4 for
// more details.
try container.encode(Decimal(numberValue))
case let .string(stringValue):
try container.encode(stringValue)
Expand Down

0 comments on commit c6157e6

Please sign in to comment.