Skip to content

Commit 2401ae2

Browse files
Add new test cases.
Improve pictures that have only '?' at the right part when applied to whole numbers.
1 parent 5bb7468 commit 2401ae2

File tree

2 files changed

+63
-8
lines changed

2 files changed

+63
-8
lines changed

dotnet/src/dotnetframework/GxClasses/Core/GXUtilsCommon.cs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -696,10 +696,11 @@ static int LeadingBlanks(string gxpicture)
696696
}
697697
return leadingBlanks;
698698
}
699-
static int TrailingBlanks(string gxpicture)
699+
static int TrailingBlanks(string gxpicture, out bool decimalsAsBlank)
700700
{
701701
int trailingBlanks = 0;
702702
int sep = gxpicture.IndexOf('.');
703+
decimalsAsBlank = false;
703704
if (sep >= 0)
704705
{
705706
string rightPic = gxpicture.Substring(sep);
@@ -709,8 +710,16 @@ static int TrailingBlanks(string gxpicture)
709710
{
710711
if (gxpicture[i] == QUESTION_MARK && !EscapedSymbol(gxpicture, i))
711712
trailingBlanks++;
712-
else if (gxpicture[i] == '.' || gxpicture[i] == NUMBER_SIGN || gxpicture[i] == 'Z' || gxpicture[i] == '9')
713+
else if (gxpicture[i] == '.')
714+
{
715+
decimalsAsBlank = true;
716+
break;
717+
}
718+
else if (gxpicture[i] == NUMBER_SIGN || gxpicture[i] == 'Z' || gxpicture[i] == '9')
719+
{
720+
decimalsAsBlank = false;
713721
break;
722+
}
714723
}
715724
}
716725
}
@@ -730,7 +739,7 @@ private static string GxPictureToNetPicture(string gxpicture, bool separatorsAsL
730739
bool explicitSign = (gxpicture[0] == '+');
731740
bool withoutMinusSign = (gxpicture[0] == '(' && gxpicture[gxpicture.Length - 1] == ')') || gxpicture.EndsWith("DB") || explicitSign;
732741
int totalLeadingBlanks = LeadingBlanks(gxpicture);
733-
int totalRighBlanks = TrailingBlanks(gxpicture);
742+
int totalRighBlanks = TrailingBlanks(gxpicture, out bool decimalsAsBlank);
734743
int lBlanks = 0;
735744
int rDigits = 0;
736745

@@ -828,7 +837,16 @@ private static string GxPictureToNetPicture(string gxpicture, bool separatorsAsL
828837
if (separatorsAsLiterals)
829838
strPicture.Append("\".\"");
830839
else
831-
strPicture.Append(gxpicture[i]);
840+
{
841+
if (decimalsAsBlank && decimals == 0)
842+
{
843+
strPicture.Append(BLANK); //Replace decimal separator by blank
844+
}
845+
else
846+
{
847+
strPicture.Append(gxpicture[i]);
848+
}
849+
}
832850
}
833851
else if (gxpicture[i] == ',')
834852
{
@@ -1299,7 +1317,7 @@ public string Format(decimal value, string gxpicture)
12991317
section = FORMAT_SECTION.ZEROS;
13001318
}
13011319
bool separatorsAsLiterals = UseLiteralSeparators(gxpicture);
1302-
string invariantStrValue = value.ToString(CultureInfo.InvariantCulture.NumberFormat);
1320+
string invariantStrValue = Math.Abs(value).ToString(CultureInfo.InvariantCulture.NumberFormat);
13031321
int decSeparatorIdx = invariantStrValue.IndexOf(CultureInfo.InvariantCulture.NumberFormat.NumberDecimalSeparator);
13041322

13051323
int digits = WholeDigits(value, invariantStrValue, decSeparatorIdx);
@@ -1342,7 +1360,11 @@ int DecimalDigits(string invariantStrValue, int decSeparatorIdx)
13421360
int WholeDigits(decimal value, string invariantStrValue, int decSeparatorIdx)
13431361
{
13441362
int digits;
1345-
if (value < 1 && value >= 0)
1363+
if (value == 0)
1364+
{
1365+
digits = 1;
1366+
}
1367+
else if (value < 1 && value >= 0)
13461368
digits = 0;
13471369
else if (decSeparatorIdx < 0)
13481370
{

dotnet/test/DotNetUnitTest/StringUtil/StringUtilTests.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,48 @@ public void TestZPicture()
110110
Assert.Equal(" 123456.12 ", decStr);
111111

112112
//=====================Zero========================================
113+
113114
decNumber = 0;
115+
116+
decStr = context.localUtil.Format(decNumber, "ZZZZZZZZZZ9.ZZZZZZ");
117+
Assert.Equal(" 0.000000", decStr);
118+
114119
decStr = context.localUtil.Format(decNumber, "###########.######");
115120
Assert.Equal(" ", decStr);
116121

117122
decStr = context.localUtil.Format(decNumber, "???????????.??????");
118123
Assert.Equal(" ", decStr);
119124

125+
decStr = context.localUtil.Format(decNumber, "(??????????9.??????)");
126+
Assert.Equal(" 0 ", decStr);
127+
128+
decStr = context.localUtil.Format(decNumber, "\\# ??????????9.??????");
129+
Assert.Equal("# 0 ", decStr);
130+
131+
decStr = context.localUtil.Format(decNumber, "(##########9.######)");
132+
Assert.Equal(" 0 ", decStr);
133+
134+
//=====================One========================================
135+
136+
decNumber = 1;
137+
138+
decStr = context.localUtil.Format(decNumber, "ZZZZZZZZZZ9.ZZZZZZ");
139+
Assert.Equal(" 1.000000", decStr);
140+
141+
decStr = context.localUtil.Format(decNumber, "###########.######");
142+
Assert.Equal(" 1", decStr);
143+
144+
decStr = context.localUtil.Format(decNumber, "???????????.??????");
145+
Assert.Equal(" 1 ", decStr);
146+
147+
decStr = context.localUtil.Format(decNumber, "(??????????9.??????)");
148+
Assert.Equal(" 1 ", decStr);
149+
120150
decStr = context.localUtil.Format(decNumber, "\\# ??????????9.??????");
121-
Assert.Equal(" # 0 ", decStr);
151+
Assert.Equal("# 1 ", decStr);
152+
153+
decStr = context.localUtil.Format(decNumber, "(##########9.######)");
154+
Assert.Equal(" 1 ", decStr);
122155

123156
//=====================0.1========================================
124157
decNumber = 0.1M;
@@ -129,7 +162,7 @@ public void TestZPicture()
129162
//=====================Negatives========================================
130163
decNumber = -123456.12M;
131164
decStr = context.localUtil.Format(decNumber, "(??????????9.??????)");
132-
Assert.Equal(" ( 123456.12 )", decStr);
165+
Assert.Equal("( 123456.12 )", decStr);
133166

134167
decStr = context.localUtil.Format(decNumber, "(##########9.######)");
135168
Assert.Equal(" (123456.12)", decStr);

0 commit comments

Comments
 (0)