Fix parsing with precision preservation#10
Merged
Merged
Conversation
### Notes Scientific notation in money strings (e.g., "1.06521485582e-7 BTC") was previously failing with "Invalid money string format" errors. This commit implements comprehensive scientific notation support with exact precision preservation for financial calculations. Key improvements: - **Manual Scientific Notation Parsing**: Replaces JavaScript's parseFloat() with custom BigInt-based parsing to avoid floating-point precision loss - **Handles Extreme Values**: Correctly parses numbers with 50+ decimal places (e.g., 1e-70) without converting them to zero - **Strict Validation**: Rejects invalid scientific notation formats like "1.23ee+5", "e+5", and "1.23e" with proper error messages - **Full Range Support**: Works with both very large (1.23E+10) and very small (1e-70) numbers while preserving exact precision Technical details: - Splits mantissa and exponent parts manually - Uses BigInt arithmetic throughout to avoid precision loss - Calculates final decimal places as (mantissaDecimals - exponent) - Maintains backward compatibility with existing number formats Fixes the original bug where "1.06521485582e-7 BTC" would throw an error, and ensures numbers like "$1e-70" preserve their exact value (amount: 1n, decimals: 70) instead of being converted to zero.
Contributor
|
Let's add some round trip tests for these numbers that we know we're getting mangled in serialization/deserialization, maybe? Or was it the Postgres |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Notes:
Scientific notation in money strings (e.g., "1.06521485582e-7 BTC") was previously failing with "Invalid money string format" errors. This commit implements comprehensive scientific notation support with exact precision preservation for financial calculations.
Key improvements:
Technical details:
Fixes the original bug where "1.06521485582e-7 BTC" would throw an error, and ensures numbers like "$1e-70" preserve their exact value (amount: 1n, decimals: 70) instead of being converted to zero.