Skip to content

Commit

Permalink
Fix numeric picture with symbol and negative value (#729)
Browse files Browse the repository at this point in the history
* Fix numeric picture with symbol and negative value
Issue: 102698

* Fix numeric picture with symbol and negative value
Issue: 102698

* Fix numeric picture with symbol and negative value
Issue: 102698
  • Loading branch information
iroqueta authored Oct 13, 2023
1 parent e521e71 commit 39df223
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 8 deletions.
38 changes: 30 additions & 8 deletions common/src/main/java/com/genexus/LocalUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -1378,7 +1378,10 @@ String alignAndPad(String text, char pad, String picture, boolean floating, Numb
}
else
{
return (alignRight(text, picture.length()));
if (text.length() < picture.length())
return (alignRight(text, picture.length()));
else
return text;
}
}

Expand Down Expand Up @@ -1443,6 +1446,7 @@ String format(long value, String picture, NumberFormat numberFormat)
int originalPictLength = picture.length();
picture = takeSymbolsFromPicture(picture);
int newPictLength = picture.length();
boolean hasSymbol = originalPictLength > newPictLength;
int j = 0;
String valueStr = String.valueOf(value);
int valueStrLength = valueStr.length();
Expand Down Expand Up @@ -1502,7 +1506,7 @@ String format(long value, String picture, NumberFormat numberFormat)
}
}

if( originalPictLength > newPictLength && !floating) formatted = addSymbolsToText(formatted, originalPicture);
if( originalPictLength > newPictLength && !floating) formatted = addSymbolsToText(formatted, originalPicture, hasSymbol);
int negativeSign = 0;
if (preffix.startsWith("("))
{
Expand All @@ -1529,7 +1533,7 @@ 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=='('))
if( (a=='Z') || (a=='9') || (a==',') || (a=='%') || (a==')') || (a=='(') || (a==' '))
{
pictureWithoutSymbols.append(a);
}
Expand Down Expand Up @@ -1559,25 +1563,42 @@ private boolean dotAsLiteral(String originalPicture)
return false;
}

private String addSymbolsToText(String text, String originalPicture)
private String addSymbolsToText(String text, String originalPicture, boolean hasSymbol)
{
StringBuffer formattedText = new StringBuffer();
int textIdx = text.length() - 1;

boolean dotAsLiteral = dotAsLiteral(originalPicture);
boolean isNegative = false;
for (int i = originalPicture.length() - 1; i >= 0;)
{
char a = originalPicture.charAt(i--);
while( (a!='Z') && (a!='9') && (a!=',') && ((a!='.') || (a=='.' && dotAsLiteral)) && (a!='%') && (a!=')') && (a!='('))
{
if (!(a =='+' && text.startsWith("-")))
formattedText.append(a);
if (a == ' ')
textIdx--;
if( i >= 0) a = originalPicture.charAt(i--);
else break;
}
if( textIdx >= 0 ) formattedText.append(text.charAt(textIdx--));
if( textIdx >= 0)
{
char textChar = text.charAt(textIdx--);
if (textChar != '-' || !hasSymbol || textIdx < 0)
formattedText.append(textChar);
else
{
formattedText.append(' ');
isNegative = true;
}
}
}
while( textIdx >= 0 ) formattedText.append(text.charAt(textIdx--));

if (isNegative)
formattedText.append('-');

String formattedString = formattedText.reverse().toString();
if (formattedString.endsWith("DB"))
{
Expand Down Expand Up @@ -1616,6 +1637,7 @@ private String formatBigDecimal(java.math.BigDecimal value, String picture, Numb
int originalPictLength = picture.length();
picture = takeSymbolsFromPicture(picture);
int newPictLength = picture.length();
boolean hasSymbol = originalPictLength > newPictLength;
int j = 0;
String valueStr = String.valueOf(value);
int valueStrLength = valueStr.length();
Expand Down Expand Up @@ -1674,13 +1696,13 @@ private String formatBigDecimal(java.math.BigDecimal value, String picture, Numb
formatted = df.format(value);
}
}
if( originalPictLength > newPictLength && !floating) formatted = addSymbolsToText(formatted, originalPicture);
if( originalPictLength > newPictLength && !floating) formatted = addSymbolsToText(formatted, originalPicture, hasSymbol);
int negativeSign = 0;
if (preffix.startsWith("("))
{
negativeSign = -1;
}
else if (value.signum() == -1)
else if (value.signum() == -1 && !originalPicture.startsWith("+"))
{
negativeSign = 1;
}
Expand Down Expand Up @@ -1785,7 +1807,7 @@ private String picturePreffix(String picture)

while(i < len)
{
if (GXPicture.isSeparator(picture.charAt(i)))
if (GXPicture.isSeparator(picture.charAt(i)) && picture.charAt(i) != ' ')
{
preffix = preffix + picture.charAt(i);
}
Expand Down
79 changes: 79 additions & 0 deletions java/src/test/java/com/genexus/TestCommonUtil.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.genexus;

import com.genexus.db.UserInformation;
import com.genexus.sampleapp.GXcfg;
import com.genexus.specific.java.Connect;
import com.genexus.specific.java.LogManager;
import org.junit.Assert;
import org.junit.Test;

import java.math.BigDecimal;
import java.util.Date;
import java.util.UUID;

Expand All @@ -13,6 +17,7 @@ private void initialize()
{
Connect.init();
LogManager.initialize(".");
Application.init(GXcfg.class);
}

@Test
Expand Down Expand Up @@ -131,6 +136,80 @@ public void testFormat() {
expectedResult = "Alex is 26 years old";
result = CommonUtil.format(value, "Alex", "26", "10", "10", "", "", "", "", "");
Assert.assertEquals(expectedResult, result);

UserInformation ui = (UserInformation) GXObjectHelper.getUserInformation(ModelContext.getModelContext(GXcfg.class), -1);
long decimalValue = -150;
String picture = "$ZZZ,ZZZ,ZZ9";
expectedResult = "-$ 150";

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

result = ui.getLocalUtil().format(DecimalUtil.doubleToDec(decimalValue), picture);
Assert.assertEquals(expectedResult, result);

picture = "$ ZZZ,ZZZ,ZZ9";
expectedResult = "-$ 150";

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

result = ui.getLocalUtil().format(DecimalUtil.doubleToDec(decimalValue), picture);
Assert.assertEquals(expectedResult, result);

picture = "ZZZ,ZZZ,ZZ9";
expectedResult = " -150";

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

result = ui.getLocalUtil().format(DecimalUtil.doubleToDec(decimalValue), picture);
Assert.assertEquals(expectedResult, result);

decimalValue = 150;
picture = "$ZZZ,ZZZ,ZZ9";
expectedResult = "$ 150";

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

decimalValue = 5;
picture = "$ 9.99";
expectedResult = "$ 5.00";

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

decimalValue = -666;
picture = "+ZZZ9";
expectedResult = "-666";

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

decimalValue = -123456789;
picture = "ZZZ,ZZZ,ZZ9";
expectedResult = "-123,456,789";

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

result = ui.getLocalUtil().format(DecimalUtil.doubleToDec(decimalValue), picture);
Assert.assertEquals(expectedResult, result);

BigDecimal bigDecimalValue = new BigDecimal(-12.5);
picture = "+99.9";
expectedResult = "-12.5";

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

bigDecimalValue = new BigDecimal(-12.5);
picture = "$Z9.9";
expectedResult = "-$12.5";

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

@Test
Expand Down

0 comments on commit 39df223

Please sign in to comment.