Skip to content

Conversation

@pavitra-infocusp
Copy link

Overview

This PR adds support for parsing formatted number strings with thousands separators directly into the from() function. This creates a complete round-trip workflow where format() can produce formatted strings and from() can parse them back without additional processing.

Motivation

The format() method returns strings with thousands separators for better readability. However, there was no way to parse these formatted strings back into Dnum values. Users had to manually remove separators before using from(). Additionally, different locales use different separators (e.g., commas in US format, dots in German format), making a locale-aware solution valuable.

Implementation

  • New parseLocaleNumber() function: Intelligently detects the system's decimal separator using (1.1).toLocaleString() and normalizes formatted strings
  • Locale detection: Automatically adapts to the environment without requiring explicit locale configuration
  • Seamless integration: The from() function now detects commas in input strings and applies locale-aware parsing

Changes

  • Modified from() to detect and parse formatted strings before validation
  • Handles various formats: "12,345.29387"12345.29387
  • Supports edge cases like mixed separators (.29,387) and negative numbers

Testing

  • Added tests for formatted strings with thousands separators
  • Tested positive and negative numbers
  • Tested decimal numbers with separators
  • Note: Comprehensive locale-specific testing (German, French, etc.) requires environment variable setup (e.g., LC_ALL='de-DE.UTF-8')

Discussion

Question for maintainers: Should we add an optional locale parameter to from() to allow users to explicitly specify the locale format, while maintaining auto-detection as the default fallback? This would provide more explicit control for edge cases while keeping the API simple for most users.

@pavitra-infocusp pavitra-infocusp changed the title feat: add locale-aware number parsing and tests for formatted strings Add locale-aware number parsing with thousands separator support Jan 7, 2026
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