Skip to content

Add custom value formatters for dates, numbers, and currency #16

@vaceslav

Description

@vaceslav

Description

Extend the format specifier system to support custom formatting for dates, numbers, and currency values.

Current State

  • Boolean formatters implemented (:checkbox, :yesno, etc.)
  • Basic architecture for format specifiers exists
  • Need to extend to other data types

Proposed Formatters

Date Formatting

{{BirthDate:yyyy-MM-dd}}      → 2025-01-15
{{DueDate:MMM dd, yyyy}}       → Jan 15, 2025
{{CreatedAt:relative}}         → 2 days ago
{{UpdatedAt:iso}}              → 2025-01-15T10:30:00Z

Number Formatting

{{Price:N2}}                   → 1,234.56
{{Percentage:P1}}              → 45.3%
{{Count:N0}}                   → 1,234
{{Scientific:E2}}              → 1.23E+03

Currency Formatting

{{Amount:C}}                   → ,234.56 (culture-aware)
{{Amount:C:EUR}}               → €1,234.56
{{Amount:C:GBP}}               → £1,234.56

Custom Formatters

var options = new DocumentProcessorOptions
{
    CustomFormatters = new Dictionary<string, IValueFormatter>
    {
        ["phone"] = new PhoneFormatter(),
        ["ssn"] = new SocialSecurityFormatter(),
        ["uppercase"] = new UpperCaseFormatter()
    }
};

Technical Design

IValueFormatter Interface

public interface IValueFormatter
{
    bool CanFormat(Type valueType, string format);
    string Format(object value, string format, CultureInfo culture);
}

Built-in Formatters

  • DateTimeFormatter
  • NumberFormatter
  • CurrencyFormatter
  • StandardFormatter (.NET format strings)

Culture Support

var options = new DocumentProcessorOptions
{
    Culture = new CultureInfo("de-DE"),
    // {{Amount:C}} → 1.234,56 € (German format)
};

Use Cases

  • Financial documents with currency
  • Reports with dates in specific formats
  • Multi-language document generation
  • Scientific/technical documents
  • Invoices and receipts

Acceptance Criteria

  • Date formatting with standard .NET format strings
  • Number formatting with standard .NET format strings
  • Currency formatting with culture support
  • Custom formatter registration
  • Culture-aware formatting
  • Fallback for invalid formats
  • Comprehensive tests
  • Documentation and examples

Priority

Medium - Highly requested feature

Backwards Compatibility

  • Fully backwards compatible
  • Format specifiers are optional
  • Existing templates continue to work

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestfeatureNew feature or requesti18nInternationalization and localization

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions