Float to Decimal in Buildop - Rounding Problem

Post questions here relative to DataStage Enterprise/PX Edition for such areas as Parallel job design, Parallel datasets, BuildOps, Wrappers, etc.

Moderators: chulett, rschirm, roy

Post Reply
vijayrc
Participant
Posts: 197
Joined: Sun Apr 02, 2006 10:31 am
Location: NJ

Float to Decimal in Buildop - Rounding Problem

Post by vijayrc »

Hi,
I have a rounding issue. Here's the scenario
All my amount fields are Decimal 15,2 and after summation ends up in Decimal 15,2 which when done with an Aggregator nicely rounds off.

Then we had to replace the Aggregator with a Buildop wherein we have defined as Float [15,2] as Input and Float [15,3] as output thinking to avoid the round off issue and finally move it into Decimal [15,2] but the round-off happens, inspite of having 15,3 in the output of Buildop which basically does the aggregattion.

Any thought on overcoming this rounding issue is greatly appreciated, as I'm a novice in C++ pgming

Thanks a bunch
Vijay
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

Where do you imagine the third decimal place is going to be generated from? You've summed numbers with a scale of 2. There is no third decimal place.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
bcarlson
Premium Member
Premium Member
Posts: 772
Joined: Fri Oct 01, 2004 3:06 pm
Location: Minnesota

Post by bcarlson »

We handle it a little differently. Input remains the original Decimal datatype. In the buildop, convert it to float on the fly, do your calculations, and reassign back to decimal on the fly. One of the optional parameters in the assignFromFloat method (part of the APT_Decimal class) is a rounding option.

Here is a simple example, where we had to change a percentage (20%) to the decimal value (.20):

Code: Select all

APT_DFloat tmpFl = 100.0;
out.int_rt.assignFromDFloat(in.X_NW_DDA_INTEREST_RATE.asDFloat() / tmpFl, APT_Decimal::eRoundInf);
Where
  • * asDFloat() converts a decimal to double float
    * assignFromDFloat converts a double float to decimal
    * APT_Decimal::eRoundInf is the type of rounding to do when reassigning back to decimal
The following is from the APT_Decimal include file on your ETL server where DataStage is installed(<your_root>/Ascential/DataStage/PXEngine/include/apt_util), and also available in the online help (I believe).

Code: Select all

  /**
     Enumeration for rounding method.
   */
  enum RoundMode
  {
    /**
       Discard material to the right of the surviving digit, regardless of
       sign.  This corresponds to the COBOL INTEGER-PART function.
       This is the default rounding mode.  Examples: 1.6 -> 1, -1.6 -> -1
     */
    eTruncZero,

    /**
       Round towards nearest representable value, breaking ties by rounding
       towards plus infinity (positive) or minus infinity (negative).  This
       corresponds to the COBOL ROUNDED phrase.
       Examples: 1.4 -> 1,   1.5 -> 2, -1.4 -> -1, -1.5 -> -2
     */
    eRoundInf,

    /**
       Truncate towards minus infinity.
       This corresponds to the IEEE 754 Round Down mode.
       Examples: 1.6 -> 1, -1.4 -> -2
     */
    eFloor,

    /**
       Truncate towards positive infinity.
       This corresponds to the IEEE 754 Round Up mode.
       Examples: 1.4 -> 2, -1.6 -> -1
     */
    eCeil

    /* not implemented: IEEE 754 Round to Nearest mode (eRoundEven) */
  };
Hope this helps.

brad.
vijayrc
Participant
Posts: 197
Joined: Sun Apr 02, 2006 10:31 am
Location: NJ

Post by vijayrc »

ray.wurlod wrote:Where do you imagine the third decimal place is going to be generated from? You've summed numbers with a scale of 2. There is no third decimal place. ...
Ray, you have a 'good' point...Yes all my input $ Charge fields are Decimal 15,2 which gets aggreagated into a Total amount which is again a Decimal 15,2 which when Aggregated with Aggregator gives the right amount in the Total. When I change the definition to Float 15,2 in the input and output, it again gives the right amount in the Total when using an Aggregator...BUT when I replace the Aggregator with a Buildop [for performance issues], using Float 15,2 as input and output, I'm off by a cent in one case....!! I have narrowed it down to be the c++ code that's doing the aggregation.. Any help is appreciated.
Thanks
Vijay
Post Reply