Skip to content

feat: support European-style decimal separators in string input #238

@TylerVigario

Description

@TylerVigario

Summary

Add optional support for European-style number formatting in string inputs, where commas are decimal separators and periods are thousands separators.

Background

Currently, n2words accepts string inputs but only recognizes JavaScript number format (period as decimal separator):

toCardinal('1234.56')  // ✓ Works: "one thousand two hundred thirty-four point five six"
toCardinal('1234,56')  // ✗ Throws: Invalid number format

Many locales use comma as the decimal separator:

  • Comma decimal: Germany, France, Spain, Italy, Brazil, most of Europe and South America
  • Period decimal: US, UK, China, Japan, Australia

Proposed API

Option 1: Per-call option

toCardinal('1.234,56', { locale: 'de' })  // German format
toCardinal('1,234.56', { locale: 'en' })  // US format (default)

Option 2: Explicit separator option

toCardinal('1.234,56', { decimalSeparator: ',' })

Option 3: Auto-detection (risky - ambiguous cases like 1,234)

toCardinal('1.234,56')  // Auto-detect European format

Considerations

  1. Ambiguity: 1,234 could be "one thousand two hundred thirty-four" (US) or "one point two three four" (EU)
  2. Complexity: Adds parsing complexity for a feature that may have limited use
  3. Alternative: Users can pre-process strings before passing to n2words
  4. Scope: Should this apply to all forms (cardinal, ordinal, currency)?

Questions to Resolve

  • Is this a common enough need to add to the library?
  • Should detection be automatic or explicit?
  • What's the priority vs other features?

Related

  • Current parsing utilities: src/utils/parse-cardinal.js, src/utils/parse-currency.js
  • String input is already supported but only in JS number format

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions