Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

imckl
Copy link

@imckl imckl commented May 20, 2020

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

@datadiode
Copy link

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 added a commit to datadiode/ReoGrid that referenced this pull request Dec 30, 2020
…s (unvell/ReoGrid PR unvell#367) / _* in number formats lead to wrong number of fractional digits (unvell/ReoGrid PR unvell#336)
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.

2 participants