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
Float to Decimal in Buildop - Rounding Problem
Moderators: chulett, rschirm, roy
-
- Participant
- Posts: 54607
- Joined: Wed Oct 23, 2002 10:52 pm
- Location: Sydney, Australia
- Contact:
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):
Where
Hope this helps.
brad.
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);
- * 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
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) */
};
brad.
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.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. ...
Thanks
Vijay