ReoGrid
ReoGrid copied to clipboard
fix: Wrong scale digit number if currency NumberDataFormatter ends with "_)"
trafficstars
Description:
A commonly used currency format defined as [$USD] #,##0.0000_);([$USD] #,##0.0000) in Excel, was wrongly parsed with two more scale digit (eg: from 1.1234 to 1.123400)
From common currency selection:

Notes:
- The bugfix just fix wrong scale digit number, and does not fully simulate with Excel behaviors. This makes sense simply for keeping things right and simple.
- Code updated with four new TestCases in A03_Styles and passed all tests.

Test case:
| Value | Format | ReoGrid | Excel |
|---|---|---|---|
| 9 | [$CNY] #,##0.00_);([$CNY] #,##0.00) | CNY 9.00 | CNY 9.00 |
| 1234 | [$CNY] #,##0.00_);([$CNY] #,##0.00) | CNY 1,234.00 | CNY 1,234.00 |
| -1.234 | [$CNY] #,##0.00_);([$CNY] #,##0.00) | -CNY 1.23 | (CNY 1.23) |
| -1.234 | [$CNY] #,##0.0000_);([$CNY] #,##0.0000) | -CNY 1.2340 | (CNY 1.2340) |
Preview:
-
Raw value:
- DataFormat:
[$USD] #,##0.0000_);([$USD] #,##0.0000) 2.749576904167612118.391509433962266-8.7510561342592581-7.6259722222222228
-
ReoGrid with fix:

-
ReoGrid without fix:

-
Excel behaviors (noticed that the values is lined up at the decimal point):

Reference:
- Official article - https://support.office.com/en-us/article/number-format-codes-5026bbd6-04bc-48cd-bf33-80f18b4eae68

- Additional info: https://bettersolutions.com/excel/formatting/number-tab-custom-format.htm

Though less commonly, you might also have more than one _* in sequence. Slightly revised approach:
int decimalSeparatorIndex = pattern.LastIndexOf(ExcelWriter.EnglishCulture.NumberFormat.NumberDecimalSeparator);
int lastDecimalIndex = pattern.LastIndexOf("0");
if (decimalSeparatorIndex >= 0 && decimalSeparatorIndex < lastDecimalIndex)
{
arg.DecimalPlaces = (short)(lastDecimalIndex - decimalSeparatorIndex);
}
else
{
arg.DecimalPlaces = 0;
}