cancel
Showing results for 
Search instead for 
Did you mean: 

User Defined Function Output

Former Member
0 Kudos

I have a UDF that converts float data into output, howevere, it add .01 in isolated cases.

The Input is as follows

<?xml version="1.0" encoding="UTF-8"?>

<ns0:MT_AMT xmlns:ns0="http://test.bob.com">

<SSN>123456789</SSN>

<DATA>PH80.00 170915.76 AAE</DATA>

</ns0:MT_AMT>

The out is as follows

<?xml version="1.0" encoding="UTF-8"?>

<ns0:MT_AMT1 xmlns:ns0="http://test.bob.com"><SSN>123456789</SSN><DATA>PH 80.00 170915.77AAE </DATA></ns0:MT_AMT1>

The code is as follows

MappingTrace mTrace = container.getTrace();

GlobalContainer globalContainer = container.getGlobalContainer();

globalContainer.setParameter("Value"," ");

//1.Map directly the source data to target data and make changes to below lengths.

StringBuffer sBuf = new StringBuffer(a);

// Correction : Make the length of sBuf equal to 21 in order to reduce the risk of runtime exception

if (sBuf.length() < 21) {

while (sBuf.length() != 21) {

sBuf.append(' ');

}

}

//2. Schedule Hours : Format the data into "ZZ9.99" at position 3-8

String substring = sBuf.substring(2, 8);

substring = formatNumber(substring, 4, 6, 2);

sBuf.replace(2, 8, substring);

//3. Base Rate : Format the data into "ZZZZZZ9.99" at position 9-29. The target position will be 9-18

substring = null;

substring = sBuf.substring(8, 29);

substring = formatNumber(substring, 8, 10, 2);

sBuf.replace(8, 18, substring.substring(0, 10));

sBuf.delete(18, 29);

//4. Performance Rating : For now no mapping is defined

//At last append 434 spaces to the record

sBuf = appendSpaces(sBuf, 434);

return sBuf.toString();

}

/*MappingTrace mTrace = container.getTrace();

/* mTrace.addInfo("Exception thrown = " + ex.getMessage() + ", Legacy Invoice Number: " + globalContainer.getParameter("INVOICENUMBER").toString() + ", " + interfaceName + ", " + lookUp + ", " + lookUpTable + ", " + keyString);

/***********************************************************************************

  • The below Function is for handling Trailer Records

***********************************************************************************

*/

private String trailerRecord(String a, int count) {

//1.Map directly the source data to target data and make changes to below lengths.

StringBuffer sBuf = new StringBuffer(a);

//2.Get the number of records from Global container and fill it at position 1-6

//Subtract 1 to exclude the trailer record

count = count - 1;

Integer counter = new Integer(count);

String substring = counter.toString();

//3.Format the number to ZZZZZ9 format

substring = formatNumber(substring, 7, 6, 0);

sBuf.replace(0, 6, substring.substring(0, 6));

//At last append 449 spaces to the record

sBuf = appendSpaces(sBuf, 449);

return sBuf.toString();

}

/***********************************************************************************

  • The below Function is for appending trailing spaces to the output record

***********************************************************************************

*/

private StringBuffer appendSpaces(StringBuffer sub, int num) {

for (int i = 0; i < num; i++) {

sub.append(' ');

}

return sub;

}

/***********************************************************************************

  • The below Function is for formatting numbers Records

***********************************************************************************

*/

private String formatNumber(

String input,

int position,

int length,

int decPlaces)

throws NumberFormatException {

//17.08.2007 : If empty string has come, send back the same

int int1 = input.lastIndexOf(' ');

int int2 = input.length();

if (int1 == int2) {

return input;

}

// position = actual position of dec place (e.g.4 for ZZ9.99)

//length = actual length (e.g. 6 for ZZ9.99)

//decPlaces = (e.g. 2 for ZZ9.99)

//First get the input converted into float and back so as to add a decimal sign if it does not exist

float f;

try {

f = Float.parseFloat(input);

} catch (NumberFormatException ex) {

throw new NumberFormatException(

"Exception Occurred:" + ex.getMessage());

}

StringBuffer sb = new StringBuffer(length);

sb.insert(0, f);

//Identify the position of decimal sign and then shift it gradually

int index = sb.indexOf(".");

if (index < position && index != -1) {

while (index < (position - 1)) {

sb.insert(0, " ");

index = sb.indexOf(".");

}

/*If there is no decimal sign, put one at the desired position

if(index == -1){

int int1 = Integer.parseInt(input);

sb.ins;

sb.insert(position,".");

}*/

}

//Fill blank spaces after decimal sign with zero

while (sb.length() < length) {

sb.append("0");

}

return sb.toString();

Any suggestions as to why this occurs? The additional .01.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

I have added Value = BigDecimal(input.toString();

When the string is passed with imbedded blanks, i get a format exception. If I fill the blanks with zeroes, It processes correctly. How should Big Decimal be coded to allow the blanks to be included?

justin_santhanam
Active Contributor
0 Kudos

Robert,

Nope it won't take care of Strings. You need to trim it before passing it. or else do something like BigDecimal((input.toString()).trim())

raj.

Answers (1)

Answers (1)

rodrigoalejandro_pertierr
Active Contributor
0 Kudos

hi

you receive a float numer and what you want to get??

or you receive a data and you convert it to float??

its no clear for me

Thanks

Rodrigo

Former Member
0 Kudos

There are 2 strings passed Socail security Number and then a second string that is parsed.

1) SSN 123456789

2)String PH80.00 170915.76 AAE

In the string the PH is separated, then the 80.00 and then the 170915.76 is formattted as float. In the UDF the 170915.76 is converted to 170915.77. It adds .01.This is incorrect. It should not add .01 to the value.

Former Member
0 Kudos

Hi,

It's correct for float. Please use BigDecimals or String.

/wg

Former Member
0 Kudos

Why is it correct for float?

Former Member
0 Kudos

Hi !

Check this:

/people/thorsten.nordholmsbirk/blog/2006/04/03/never-ever-use-xis-built-in-arithmetic-functions

Regards,

Matias

ps: please award points if helpful

Former Member
0 Kudos

Of course you cannot use String as float.

Remember: Floating arithmetic is always 2 power to something and is always an approximation of the number. Never use it in calculations.

/wg