on 02-25-2008 2:13 PM
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.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
User | Count |
---|---|
84 | |
10 | |
10 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.