ReoGrid icon indicating copy to clipboard operation
ReoGrid copied to clipboard

fix: Wrong scale digit number if currency NumberDataFormatter ends with "_)"

Open imckl opened this issue 5 years ago • 1 comments
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:

Format Selection

Notes:

  1. 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.
  2. Code updated with four new TestCases in A03_Styles and passed all tests. UnitTest1 UnitTest2

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:

  1. DataFormat: [$USD] #,##0.0000_);([$USD] #,##0.0000)
  2. 2.7495769041676121
  3. 18.391509433962266
  4. -8.7510561342592581
  5. -7.6259722222222228
  • ReoGrid with fix:

ReoGrid with fix

  • ReoGrid without fix:

ReoGrid without fix

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

Excel behaviors

Reference:

  1. Official article - https://support.office.com/en-us/article/number-format-codes-5026bbd6-04bc-48cd-bf33-80f18b4eae68 Official
  2. Additional info: https://bettersolutions.com/excel/formatting/number-tab-custom-format.htm Additional

imckl avatar May 20 '20 08:05 imckl

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;
}

datadiode avatar Dec 30 '20 14:12 datadiode