Skip to content

feat: add number formatting options for display (#197)#203

Open
wellorbetter wants to merge 1 commit intoYS-L:mainfrom
wellorbetter:feat/number-formatting
Open

feat: add number formatting options for display (#197)#203
wellorbetter wants to merge 1 commit intoYS-L:mainfrom
wellorbetter:feat/number-formatting

Conversation

@wellorbetter
Copy link
Copy Markdown

Description

Add number formatting options for display rendering.

Closes #197

Reasoning

CSV files often contain raw numeric data that is hard to read at a glance (e.g. 23.505744680851063, 1234567, 2500000000).
This change adds display-layer formatting so numbers can be rendered in a more human-readable form without affecting the
underlying data — search, sort, and copy all continue to use original values.

Four formats are supported:

Format Flag Example input Example output
thousands --number-format thousands 1234567.89 1,234,567.89
fixed --number-format fixed 23.505744680851063 23.51
si --number-format si 2500000000 2.50B
scientific --number-format scientific 0.00123 1.23e-3

Non-numeric values (dates, strings, empty cells) are passed through unchanged.

An optional --precision N parameter controls decimal places for fixed, si, and scientific formats (default: 2).

Usage

# Apply one format to all columns
csvlens data.csv --number-format fixed --precision 2

# Per-column format
csvlens data.csv --column-format "revenue=thousands" --column-format "rate=fixed" --precision 3

# SI units (useful for large counts)
csvlens data.csv --number-format si

# Precision alone has no effect (warns the user)
csvlens data.csv --precision 2
# Warning: --precision has no effect without --number-format or --column-format

Testing

  1. Run cargo test — 40 new unit tests in src/format.rs, all passing
  2. Open examples/test_format.csv with each format and verify display:
    target/release/csvlens examples/test_format.csv --number-format fixed --precision 2
    target/release/csvlens examples/test_format.csv --number-format thousands
    target/release/csvlens examples/test_format.csv --number-format si
    target/release/csvlens examples/test_format.csv --number-format scientific --precision 2
    target/release/csvlens examples/test_format.csv --column-format "value=fixed" --column-format "count=thousands" --precision 3
  3. Confirm search/filter on formatted columns still uses original values
  4. Confirm non-numeric columns (e.g. date) render unchanged
  5. Confirm --no-headers + --column-format prints a warning

Type of change

  • Bug fix (A non-breaking change that fixes an issue)
  • New feature (A non-breaking change that adds functionality)
  • Breaking change (A fix or feature that would cause existing functionality to not work as expected)
  • Refactor (A code change that neither fixes a bug nor adds a feature)
  • Performance (A code change that improves performance)
  • Style (Code style changes)
  • Docs (Changes to documentation)
  • Chore (Changes to the build process or other tooling)

Copy link
Copy Markdown
Author

@wellorbetter wellorbetter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implements number formatting for display rendering (issue #197).

New module src/format.rs provides four formats — thousands, fixed, si, scientific —with an optional --precision N parameter. Formatting is applied at the render layer only
(src/ui.rs), so search/sort/copy continue to use original values.

Changes:

  • src/format.rs: core formatting logic + 40 unit tests
  • src/runner.rs: three new CLI args (--number-format, --column-format, --precision)
  • src/app.rs: pass config as Arc<ColumnFormatConfig> to render state each frame
  • src/ui.rs: apply format in render_row and get_column_widths for correct column sizing
  • src/lib.rs: expose pub mod format

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.

Human formatting of numbers

1 participant