EBIFour.com Training Clarify QRGs Java - How-to Right Justify Numeric Value in Ruleset
Java - How-to Right Justify Numeric Value in Ruleset
9th April 2020
As part of our Cleo Clarify JAVA V2 series, we are teaching users different ways to use Cleo Clarify JAVA Ruleset Actions.
JAVA Scenario:
The Cleo Clarify developer wants to right justify a numeric value and return value as a string. This is a possible scenario when the source field is numeric and the target is a string field.
The JAVA program (below) will accept 4 input values and will return one output value
JAVA Set-up
Example 1: Right Justified output with no decimals
Inputs: input: 123, length: 10, decimals: 0
Result: “ 123”
Example 2: Right justified output with decimals
Inputs: input: 123, length: 10, decimals: 2
Result: “ 123.00”
Example 3: Input with decimals and more precision needed
Inputs: input: 123.12, length: 10, decimals: 4
Result: “ 123.1200”
Example 4: Input with decimals - but no decimal length specified
Inputs: input: 123.12, length: 10, decimals: 0
Result: “ 123”
JAVA Program:
/*** @author AR
package com.abc.core;
import java.math.BigDecimal;
import java.util.Formatter;
import com.extol.ebi.lang.annotations.EbiName;
import com.extol.ebi.reactor.lib.AbstractAction;
@EbiName(“Number Right justify - Returns String”)
public class NumberRightJustify extends AbstractAction{
String outputStr = “”;
int strLength = 1; //default to at least 1 char
int decimalLength = 0;
BigDecimal inputVal;
Formatter fmt;
//com.extol.ebi.ruleset.lang.core.String output;
public com.extol.ebi.ruleset.lang.core.String execute (
@EbiName(“input”) com.extol.ebi.ruleset.lang.core.Number input,
@EbiName(“length”) com.extol.ebi.ruleset.lang.core.Number length,
@EbiName(“decimals”) com.extol.ebi.ruleset.lang.core.Number decimals
) {
//get field length only when provided
if (length != null) {
strLength = length.asJavaInteger().intValue();
}
//Check if output needs decimals
if (decimals !=null) {
decimalLength = decimals.asJavaInteger().intValue();
}
if(input !=null) {
inputVal = new BigDecimal(input.asBigDecimal().toString());
fmt = new Formatter();
fmt.format(“%”+strLength+“.”+decimalLength+“f”, inputVal);
outputStr = fmt.toString();
}
return this.asString(outputStr);
}
}
By: Sean Hoppe on