Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common/src/main/java/com/genexus/GXPicture.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,6 @@ public String nullMask()

public static boolean isSeparator(char c)
{
return (c != '9' && c != 'X' && c != '!' && c != 'Z' && c != 'A' && c != 'M');
return (c != '?' && c != '#' && c != '9' && c != 'X' && c != '!' && c != 'Z' && c != 'A' && c != 'M');
}
}
114 changes: 92 additions & 22 deletions common/src/main/java/com/genexus/LocalUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -1361,6 +1361,12 @@ public String pictureToDateFormat(String picture)

String alignAndPad(String text, char pad, String picture, boolean floating, NumberFormat numberFormat, String originalPicture)
{
String signPrefix = "";
if (text.startsWith("-") && picture.startsWith("?"))
{
signPrefix = "-";
text = text.substring(1);
}
DecimalFormat df = (DecimalFormat) numberFormat;
char decimalSeparator = df.getDecimalFormatSymbols().getDecimalSeparator();

Expand Down Expand Up @@ -1389,9 +1395,11 @@ String alignAndPad(String text, char pad, String picture, boolean floating, Numb

StringBuffer sText = new StringBuffer(text);

if (!CommonUtil.in(text, decimalSeparator) )

if (!CommonUtil.in(text, decimalSeparator))
{
sText.append(decimalSeparator);
if (picture.charAt(posSep +1) != '#' && picture.charAt(posSep +1) != '?')
sText.append(decimalSeparator);
posText = sText.length() - 1;
}

Expand All @@ -1404,8 +1412,18 @@ String alignAndPad(String text, char pad, String picture, boolean floating, Numb
sText.append(pad);
}*/

while (sText.length() < picture.length() )
sText.append(pad);
int offset = 0;
while (sText.length() + offset < picture.length() )
{
char charAt = picture.charAt(sText.length());
if (charAt == '?')
sText.append(' ');
else
if (charAt != '#')
sText.append(pad);
else
offset ++;
}


if (floating || originalPicture.startsWith("$"))
Expand All @@ -1414,7 +1432,7 @@ String alignAndPad(String text, char pad, String picture, boolean floating, Numb
}
else
{
return sText.toString();
return signPrefix + sText.toString();
}
}

Expand Down Expand Up @@ -1495,7 +1513,7 @@ String format(long value, String picture, NumberFormat numberFormat)
if ((!isAllZ(picture) || value != 0) && continua)
{
DecimalFormat df = (DecimalFormat) numberFormat;
df.applyPattern(PictureFormatter.pictureToNumberFormat(withoutSuffixPicture.replace('9','0').replace('Z','#')));
df.applyPattern(pictureToNumberFormat(withoutSuffixPicture));
if ( CommonUtil.in(picture, '.') || CommonUtil.in(picture, ','))
{
formatted = alignAndPad(df.format(value), '0', withoutSuffixPicture, floating, numberFormat, originalPicture);
Expand Down Expand Up @@ -1533,14 +1551,18 @@ private String takeSymbolsFromPicture(String picture)
for (int i = 0; i < picture.length(); i++)
{
char a = picture.charAt(i);
if( (a=='Z') || (a=='9') || (a==',') || (a=='%') || (a==')') || (a=='(') || (a==' '))
if( (a=='?') || (a=='#') || (a=='Z') || (a=='9') || (a==',') || (a=='%') || (a==')') || (a=='(') || (a==' '))
{
pictureWithoutSymbols.append(a);
}
else if (a=='.' && !dotRemove)
{
pictureWithoutSymbols.append(a);
}
else if (a=='\\')
{
i++;
}
}
return pictureWithoutSymbols.toString();
}
Expand Down Expand Up @@ -1667,6 +1689,7 @@ private String formatBigDecimal(java.math.BigDecimal value, String picture, Numb
}

String preffix = picturePreffix(picture);
String suffix = pictureSuffix(picture);
if (preffix.startsWith("("))
{
if (value.compareTo(java.math.BigDecimal.ZERO) == -1)
Expand All @@ -1677,16 +1700,17 @@ private String formatBigDecimal(java.math.BigDecimal value, String picture, Numb
{
picture = picture.replace('(', 'Z').substring(0, picture.length() - 1);
preffix = "";
suffix = "";
}
}
boolean floating = (preffix.trim().length() > 1 && preffix.replace(preffix.charAt(0), new String(" ").charAt(0)).trim().equals(""));

String withoutSuffixPicture = removePictureSuffix("%", removePicturePreffix(preffix,picture));
String withoutSuffixPicture = removePictureSuffix(suffix, removePicturePreffix(preffix,picture));

if ((!isAllZ(picture) || value.compareTo(java.math.BigDecimal.ZERO) != 0) && continua)
{
DecimalFormat df = (DecimalFormat) numberFormat;
df.applyPattern(PictureFormatter.pictureToNumberFormat(withoutSuffixPicture.replace('9','0').replace('Z','#')));
df.applyPattern(pictureToNumberFormat(withoutSuffixPicture));
if ( CommonUtil.in(picture, '.') || CommonUtil.in(picture, ','))
{
formatted = alignAndPad(df.format(value), '0', withoutSuffixPicture, floating, numberFormat, originalPicture);
Expand All @@ -1696,23 +1720,31 @@ private String formatBigDecimal(java.math.BigDecimal value, String picture, Numb
formatted = df.format(value);
}
}
if( originalPictLength > newPictLength && !floating) formatted = addSymbolsToText(formatted, originalPicture, hasSymbol);
if( originalPictLength > newPictLength && !floating && !originalPicture.startsWith("\\")) formatted = addSymbolsToText(formatted, originalPicture, hasSymbol);
int negativeSign = 0;
if (preffix.startsWith("("))
{
negativeSign = -1;
negativeSign = -2;
}
else if (value.signum() == -1 && !originalPicture.startsWith("+"))
{
negativeSign = 1;
}
if (floating)
{
return alignRight(preffix.substring(preffix.length() -1) + addPictureSuffix("%", originalPicture, addPicturePreffix(preffix, originalPicture, formatted)), originalPictLength + negativeSign);
return alignRight(preffix.substring(preffix.length() -1) + addPictureSuffix(suffix, originalPicture, addPicturePreffix(preffix, originalPicture, formatted)), originalPictLength + negativeSign);
}
else
{
return addPictureSuffix("%", originalPicture, addPicturePreffix(preffix, originalPicture, alignRight(formatted, originalPictLength + negativeSign)));
if (preffix.equals("(") && originalPicture.charAt(1) == '#')
return alignRight(addPictureSuffix(suffix, originalPicture, addPicturePreffix(preffix, originalPicture, CommonUtil.ltrim(formatted))), originalPictLength);

if (originalPicture.startsWith("\\"))
{
negativeSign = -2;
return originalPicture.charAt(1) + addPictureSuffix(suffix, originalPicture, addPicturePreffix(preffix, originalPicture, alignRight(formatted, originalPictLength + negativeSign)));
}
return addPictureSuffix(suffix, originalPicture, addPicturePreffix(preffix, originalPicture, alignRight(formatted, originalPictLength + negativeSign)));
}
}

Expand All @@ -1725,7 +1757,10 @@ private String format(double value, String picture, NumberFormat numberFormat)
String formatted = "";
String originalPicture = picture;

if (picture.startsWith("\\"))
picture = picture.substring(2);
String preffix = picturePreffix(picture);
String suffix = pictureSuffix(picture);
if (preffix.startsWith("("))
{
if (value < 0)
Expand All @@ -1734,18 +1769,19 @@ private String format(double value, String picture, NumberFormat numberFormat)
}
else
{
picture = picture.replace('(', 'Z').substring(0, picture.length() - 1);
picture = picture.replace('(', 'Z').replace(')', '?');
preffix = "";
suffix = "";
}
}
boolean floating = (preffix.trim().length() > 1 && preffix.replace(preffix.charAt(0), new String(" ").charAt(0)).trim().equals(""));

String withoutSuffixPicture = removePictureSuffix("%", removePicturePreffix(preffix,picture));
String withoutSuffixPicture = removePictureSuffix(suffix, removePicturePreffix(preffix,picture));

int negativeSign = 0;
if (preffix.startsWith("("))
{
negativeSign = -1;
negativeSign = -2;
}
else if (value < 0)
{
Expand All @@ -1754,28 +1790,36 @@ else if (value < 0)
if (!isAllZ(picture) || value != 0)
{
DecimalFormat df = (DecimalFormat) numberFormat;
df.applyPattern(PictureFormatter.pictureToNumberFormat(withoutSuffixPicture.replace('9','0').replace('Z','#')));
df.applyPattern(pictureToNumberFormat(withoutSuffixPicture));
if ( CommonUtil.in(picture, '.') || CommonUtil.in(picture, ','))
{
if (floating)
{
return alignRight(preffix.substring(preffix.length() -1) + addPictureSuffix("%", picture, addPicturePreffix(preffix, picture, alignAndPad(df.format(value), '0', withoutSuffixPicture, floating, numberFormat, originalPicture))), picture.length()+negativeSign);
return alignRight(preffix.substring(preffix.length() -1) + addPictureSuffix(suffix, picture, addPicturePreffix(preffix, picture, alignAndPad(df.format(value), '0', withoutSuffixPicture, floating, numberFormat, originalPicture))), picture.length()+negativeSign);
}
else
{
return addPictureSuffix("%", picture, addPicturePreffix(preffix, picture, alignAndPad(df.format(value), '0', withoutSuffixPicture, floating, numberFormat, originalPicture)));
if (preffix.equals("(") && picture.charAt(1) == '#')
return alignRight(addPictureSuffix(suffix, picture, addPicturePreffix(preffix, picture, CommonUtil.ltrim(alignAndPad(df.format(value), '0', withoutSuffixPicture, floating, numberFormat, originalPicture)))), picture.length());

if (originalPicture.startsWith("\\"))
{
return originalPicture.charAt(1) + addPictureSuffix(suffix, picture, addPicturePreffix(preffix, picture, alignRight(alignAndPad(df.format(value), '0', withoutSuffixPicture, floating, numberFormat, originalPicture), picture.length()+negativeSign)));
}

return addPictureSuffix(suffix, picture, addPicturePreffix(preffix, picture, alignRight(alignAndPad(df.format(value), '0', withoutSuffixPicture, floating, numberFormat, originalPicture), picture.length()+negativeSign)));
}
}
formatted = df.format(value);
}

if (floating)
{
return alignRight(preffix.substring(preffix.length() -1) + addPictureSuffix("%", picture, addPicturePreffix(preffix, picture, formatted)), picture.length()+negativeSign);
return alignRight(preffix.substring(preffix.length() -1) + addPictureSuffix(suffix, picture, addPicturePreffix(preffix, picture, formatted)), picture.length()+negativeSign);
}
else
{
return addPictureSuffix("%", picture, addPicturePreffix(preffix, picture, alignRight(formatted, picture.length()+negativeSign)));
return addPictureSuffix(suffix, picture, addPicturePreffix(preffix, picture, alignRight(formatted, picture.length()+negativeSign)));
}
}

Expand Down Expand Up @@ -1807,7 +1851,7 @@ private String picturePreffix(String picture)

while(i < len)
{
if (GXPicture.isSeparator(picture.charAt(i)) && picture.charAt(i) != ' ')
if (GXPicture.isSeparator(picture.charAt(i)) && picture.charAt(i) != ' ' && picture.charAt(i) != '\\')
{
preffix = preffix + picture.charAt(i);
}
Expand All @@ -1820,6 +1864,27 @@ private String picturePreffix(String picture)
return preffix;
}

private String pictureSuffix(String picture)
{
int i = picture.length() -1;
int len = 0;
String preffix = "";

while(i > len)
{
if (GXPicture.isSeparator(picture.charAt(i)) && picture.charAt(i) != ' ')
{
preffix = preffix + picture.charAt(i);
}
else
{
return preffix;
}
i--;
}
return preffix;
}

private String removePicturePreffix(String suffix, String picture)
{
if (picture.startsWith(suffix))
Expand All @@ -1840,6 +1905,11 @@ private String addPicturePreffix(String suffix, String originalPicture, String v
return value;
}

private String pictureToNumberFormat(String withoutSuffixPicture)
{
return PictureFormatter.pictureToNumberFormat(withoutSuffixPicture.replace('9','0').replace('Z','#'));
}

public long ctol(String value)
{
if(value == null)return 0;
Expand Down
19 changes: 17 additions & 2 deletions common/src/main/java/com/genexus/PictureFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,26 @@ public static String pictureToNumberFormat(String picture)
char car = picture.charAt(i);
switch (car)
{
case '\\' :
newPicture.append('\'');
newPicture.append(picture.charAt(i +1));
newPicture.append('\'');
newPicture.append(picture.charAt(i +1));
i++;
break;
case '?' :
newPicture.append('#');
break;
case '9' :
newPicture.append('#');
break;
case 'Z' :
newPicture.append('#');
break;
case '.' :
if (i > 0)
if (i > 0 && picture.charAt(i -1) != '?')
{
newPicture.setCharAt(i-1, '0');
newPicture.setCharAt(newPicture.length()-1, '0');
}
newPicture.append(car);
if(i+1 < picture.length() &&
Expand All @@ -105,6 +115,11 @@ public static String pictureToNumberFormat(String picture)
newPicture.append('0');
i++;
}
else if (picture.charAt(i +1) == '?' && picture.charAt(i -1) == '?')
{
newPicture.append('0');
i++;
}

break;
case '$':
Expand Down
27 changes: 27 additions & 0 deletions java/src/test/java/com/genexus/TestCommonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,35 @@ public void testFormat() {

result = ui.getLocalUtil().format(bigDecimalValue, picture);
Assert.assertEquals(expectedResult, result);

double doubleValue = 123456.12;
doLocalUtilFormatAssert(ui, doubleValue, new BigDecimal(doubleValue), "ZZZZZZZZZZ9.ZZZZZZ", " 123456.120000");
doLocalUtilFormatAssert(ui, doubleValue, new BigDecimal(doubleValue), "99999999999.999999", "00000123456.120000");
doLocalUtilFormatAssert(ui, doubleValue, new BigDecimal(doubleValue), "ZZZZZZZZZZ9.999999", " 123456.120000");
doLocalUtilFormatAssert(ui, doubleValue, new BigDecimal(doubleValue), "##########9.######", " 123456.12");
doLocalUtilFormatAssert(ui, doubleValue, new BigDecimal(doubleValue), "??????????9.??????", " 123456.12 ");
doLocalUtilFormatAssert(ui, doubleValue, new BigDecimal(doubleValue), "\\# ??????????9.??????", "# 123456.12 ");
doLocalUtilFormatAssert(ui, doubleValue, new BigDecimal(doubleValue), "##,###,###,##9.######", " 123,456.12");
doLocalUtilFormatAssert(ui, doubleValue, new BigDecimal(doubleValue), "??,???,???,??9.??????", " 123,456.12 ");

doubleValue = 0;
doLocalUtilFormatAssert(ui, doubleValue, new BigDecimal(doubleValue), "###########.######", " ");
doLocalUtilFormatAssert(ui, doubleValue, new BigDecimal(doubleValue), "???????????.??????", " ");
doLocalUtilFormatAssert(ui, doubleValue, new BigDecimal(doubleValue), "\\# ??????????9.??????", "# 0 ");

doubleValue = 0.1;
doLocalUtilFormatAssert(ui, doubleValue, new BigDecimal(doubleValue), "???????????.??????", " .1 ");

doubleValue = -123456.12;
doLocalUtilFormatAssert(ui, doubleValue, new BigDecimal(doubleValue), "(??????????9.??????)", "( 123456.12 )");
}

private void doLocalUtilFormatAssert(UserInformation ui, double doubleValue, BigDecimal bigDecimalValue, String picture, String expectedResult) {
String result = ui.getLocalUtil().format(doubleValue, picture);
Assert.assertEquals(expectedResult, result);
result = ui.getLocalUtil().format(bigDecimalValue, picture);
Assert.assertEquals(expectedResult, result);
}
@Test
public void testConvertObjectTo() {
initialize();
Expand Down