pjfanning opened a new pull request, #1406: URL: https://github.com/apache/poi/pull/1406
A fraction format can give the denominator as a number, e.g. `# ?/4` or `#\ ??/100`. `CellFormat` didn't support this: - **`?/2`, `?/4`, `?/8`, `??/16`:** `CellNumberFormatter` found no digit placeholders after the `/` and dropped the fraction. Then `writeSingleInteger` failed with a `NullPointerException`, which was caught and logged as `error while fraction evaluation`, and the raw `?/2` was left in the output. - **`?/10`, `??/100`:** the zeros were taken for digit placeholders, giving e.g. `0/11` and `0/101`. ## Changes - **`CellNumberPartHandler`** reads the digits straight after the `/` as a fixed denominator, including any zeros. A marker `Special` records where the denominator ends. - **`CellNumberFormatter`** uses the fixed denominator, without reducing the fraction, so 2/4 stays 2/4. The numerator is `round(fraction × denominator)`. - **A fraction that rounds to 0, or up to a whole number**, now shows only the integer part, as Excel does. Previously `# ?/?` showed 0.000463 as `0/1` instead of `0`, and `# ?/2` showed 0.9 as `2/2` instead of `1`. This applies to variable denominators too. ## Verification `54686_fraction_formats.xls` (saved by Excel, from bug 54686) and `54686_fraction_formats.txt` (the text Excel shows for each cell, exported as tab-delimited text with the `?` padding) have 354 values in each of these fixed-denominator columns: | Format | `CellFormat` before | after | |---|---|---| | `#\ ?/2`, `#\ ?/4`, `#\ ?/8`, `#\ ??/16` | 0 | 354 each | | `#\ ?/10`, `#\ ??/100` | 3 | 354 each | `TestCellFormat.testFixedDenominatorFractionsMatchExcel` now checks all of these, including the spaces the `?` placeholders give. Across the whole truth file, no cell gets worse and 2,207 get better. That includes the variable-denominator columns `# ?/?` (234 → 315) and `# ??/??` (309 → 316), from the change for fractions that round to 0 or 1. ## Not fixed here For variable denominators, two problems remain: - `SimpleFraction.buildFractionMaxDenominator` picks `0/1` for 0.01 and `1/1` for 0.99 with a maximum of 99, where Excel shows `1/99` and `98/99`. - `?` padding in a variable denominator is on the wrong side: `1/ 20` where Excel shows `1/20 `. These cause the remaining mismatches in the `# ??/??` and `#\ ???/???` columns. ## Tests Passing locally: - `org.apache.poi.ss.format.*`, `TestDataFormatter`, `TestFractionFormat`, `TestHSSFDataFormatter` - `TestDataFormatterAgainstExcel` (no change in which cases match), `org.apache.poi.ss.tests.format.*`, `TestXSSFDataFormat` - forbidden-apis 🤖 Generated with [Claude Code](https://claude.com/claude-code) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
