Fix formula format specifier for #.### and 0.00E00 patterns - #81
Merged
Conversation
…#.###, 0.00E00)
The @{expr | pattern} formula format specifier already worked for simple
fixed-decimal patterns like 0.000/0.0000 (matching Java Funz's Formula.java
which delegates to java.text.DecimalFormat), but patterns using '#' digits
or scientific notation ('E') were silently mishandled: '#.###' behaved like
a fixed-decimal pattern (no trailing-zero stripping) and '0.00E00' produced
garbage (decimal count taken from all characters after the first '.',
ignoring the E-exponent entirely).
Add _format_decimal_pattern()/_format_number() helpers implementing the
relevant DecimalFormat subset (0 = zero-padded digit, # = significant-only
digit, E = scientific notation with configurable exponent digits) and use
them in both the Python and R code paths of evaluate_formulas() and
evaluate_single_formula().
Also documents the formula number-formatting feature in
doc/formulas-and-interpreters.md and doc/INDEX.md, which previously had no
user-facing documentation despite being implemented and tested.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes and extends Java Funz–compatible formula number formatting (@{expr | pattern}) in fz/interpreter.py, aligning FZ’s behavior with the relevant java.text.DecimalFormat pattern subset (notably #.### and 0.00E00), and documents the feature.
Changes:
- Add DecimalFormat-like formatting helpers (
_format_decimal_pattern(),_format_number()) and use them in bothevaluate_formulas()(template substitution) andevaluate_single_formula()(typed return path). - Add compatibility tests for
#.###(strip trailing zeros) and0.00E00(scientific notation). - Document the formatting feature and add an Unreleased NEWS entry.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
fz/interpreter.py |
Implements DecimalFormat-like formatting and wires it into Python/R formula evaluation paths. |
tests/test_java_funz_compatibility.py |
Adds regression tests for #.### and 0.00E00 formatting patterns. |
doc/formulas-and-interpreters.md |
Documents number-format patterns for formulas with examples. |
doc/INDEX.md |
Adds an index entry pointing to the new documentation section. |
NEWS.md |
Adds release notes describing the expanded formatting support. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+69
to
+92
| match = re.match(r'^(?P<mantissa>[0#]*(?:\.[0#]*)?)[Ee](?P<exp>[0#]+)$', format_spec) | ||
| if match: | ||
| mantissa_pattern = match.group('mantissa') or '0' | ||
| exp_digits = len(match.group('exp')) | ||
| frac_pattern = mantissa_pattern.split('.', 1)[1] if '.' in mantissa_pattern else '' | ||
| mantissa_decimals = len(frac_pattern) | ||
|
|
||
| if value == 0: | ||
| mantissa, exponent = 0.0, 0 | ||
| else: | ||
| exponent = int(math.floor(math.log10(abs(value)))) | ||
| mantissa = value / (10 ** exponent) | ||
| mantissa = round(mantissa, mantissa_decimals) | ||
| if abs(mantissa) >= 10: | ||
| mantissa /= 10 | ||
| exponent += 1 | ||
| elif abs(mantissa) < 1: | ||
| mantissa *= 10 | ||
| exponent -= 1 | ||
|
|
||
| mantissa_str = _format_decimal_pattern(mantissa, mantissa_pattern) | ||
| sign = '-' if exponent < 0 else '' | ||
| return f"{mantissa_str}E{sign}{abs(exponent):0{exp_digits}d}" | ||
|
|
Comment on lines
+128
to
+134
| content = ( | ||
| "A: @{123456.789 | 0.00E00}\n" | ||
| "B: @{0.000123456 | 0.00E00}" | ||
| ) | ||
| result = evaluate_formulas(content, model, {}, interpreter="python") | ||
| assert "A: 1.23E05" in result | ||
| assert "B: 1.23E-04" in result |
Comment on lines
+114
to
+118
| content = "A: @{3.14159 | #.###}\nB: @{3.1 | #.###}\nC: @{3.0 | #.###}" | ||
| result = evaluate_formulas(content, model, {}, interpreter="python") | ||
| assert "A: 3.142" in result | ||
| assert "B: 3.1" in result | ||
| assert "C: 3" in result |
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.
Context
Java Funz's formula format specifier (
@{expr | pattern}, seeFormula.java) delegates directly tojava.text.DecimalFormat. FZ already supported the basic fixed-decimal case (@{expr | 0.000}), but two other DecimalFormat pattern styles were silently broken:#.###(significant-digit / trailing-zero-stripping patterns) was treated like0.000(fixed decimals, no stripping):@{3.1 | #.###}produced3.100instead of3.1.0.00E00(scientific notation) was completely mishandled: the code counted all characters after the first.as decimal places, ignoring theEexponent entirely, producing nonsensical output like123456.78900instead of1.23E05.Changes
fz/interpreter.py: added_format_decimal_pattern()and_format_number()helpers implementing the relevantDecimalFormatsubset:0→ always-shown, zero-padded digit#→ digit shown only if significant (trailing zeros stripped)E→ scientific notation, with exponent digit count taken from the patternevaluate_formulas()(string substitution into template content) andevaluate_single_formula()(typed return value).tests/test_java_funz_compatibility.py: addedtest_formula_with_hash_pattern_strips_trailing_zerosandtest_formula_with_scientific_format.doc/formulas-and-interpreters.md/doc/INDEX.md: documented the formula number-formatting feature (previously implemented and tested but undocumented for users).NEWS.md: release note.Verification