Skip to content

Commit 4200c63

Browse files
authored
Merge pull request #1710 from nissl-lab/copilot/fix-npoi-phone-number-format
Fix phone number format: escaped dashes misidentified as grouping chars; decimal cast in special format classes
2 parents 377afc7 + a9db3c1 commit 4200c63

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

main/SS/UserModel/DataFormatter.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -894,12 +894,15 @@ public override object ParseObject(string source, int pos)
894894

895895
private FormatBase CreateNumberFormat(string formatStr, double cellValue)
896896
{
897+
// Check for alternate grouping BEFORE cleaning (backslash stripping) so that
898+
// escaped literal characters like \- are not misidentified as grouping chars.
899+
// eg for a format like #'##0 which wants 12'345 not 12,345
900+
Match agm = alternateGrouping.Match(formatStr);
901+
897902
string format = cleanFormatForNumber(formatStr);
898903
NumberFormatInfo symbols = decimalSymbols;
899904

900905
// Do we need to change the grouping character?
901-
// eg for a format like #'##0 which wants 12'345 not 12,345
902-
Match agm = alternateGrouping.Match(format);
903906
if (agm.Success)
904907
{
905908
char grouping = agm.Groups[2].Value[0];

main/SS/Util/Format.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private SSNFormat()
5858
/** Format a number as an SSN */
5959
public override string Format(object obj, CultureInfo culture)
6060
{
61-
var result = ((double)obj).ToString(df, culture);
61+
var result = Convert.ToDouble(obj, CultureInfo.InvariantCulture).ToString(df, culture);
6262
var sb = new StringBuilder();
6363
sb.Append(result.Substring(0, 3)).Append('-');
6464
sb.Append(result.Substring(3, 2)).Append('-');
@@ -99,7 +99,7 @@ private ZipPlusFourFormat()
9999
/** Format a number as Zip + 4 */
100100
public override string Format(object obj, CultureInfo culture)
101101
{
102-
var result = ((double)obj).ToString(df, culture);
102+
var result = Convert.ToDouble(obj, CultureInfo.InvariantCulture).ToString(df, culture);
103103
return result.Substring(0, 5)+'-'+result.Substring(5, 4);
104104
}
105105

@@ -137,7 +137,7 @@ private PhoneFormat()
137137
/** Format a number as a phone number */
138138
public override string Format(object obj, CultureInfo culture)
139139
{
140-
var result = ((double)obj).ToString(df, culture);
140+
var result = Convert.ToDouble(obj, CultureInfo.InvariantCulture).ToString(df, culture);
141141
var sb = new StringBuilder();
142142
String seg1, seg2, seg3;
143143
var len = result.Length;

testcases/main/SS/UserModel/TestDataFormatter.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,19 @@ Excel displays the month instead of minutes."
894894
* bug 60422 : DataFormatter has issues with a specific NumberFormat in Germany default locale
895895
* Currently, this test only passes if you set LocaleUtil.setUserLocale(Locale.ROOT) or Locale.US.
896896
*/
897+
[Test]
898+
public void TestPhoneNumberFormat()
899+
{
900+
DataFormatter formatter = new DataFormatter(CultureInfo.GetCultureInfo("en-US"));
901+
902+
// Bug: alternateGrouping regex should not misidentify escaped literal '-' as a grouping character
903+
ClassicAssert.AreEqual("(123) 456-7890", formatter.FormatRawCellContents(1234567890, -1, "[<=9999999]###\\-####;\\(###\\)\\ ###\\-####"));
904+
ClassicAssert.AreEqual("123-456-7890", formatter.FormatRawCellContents(1234567890, -1, "###\\-###\\-####"));
905+
906+
// Bug: SSNFormat/ZipPlusFourFormat/PhoneFormat must handle decimal values passed by FormatRawCellContents
907+
ClassicAssert.AreEqual("(123) 456-7890", formatter.FormatRawCellContents(1234567890, -1, "[<=9999999]###-####;(###) ###-####"));
908+
}
909+
897910
[Test]
898911
public void TestBug60422()
899912
{

0 commit comments

Comments
 (0)