Function to round a decimal to highest possible integer
Moderators: chulett, rschirm, roy
-
- Charter Member
- Posts: 143
- Joined: Thu Nov 04, 2004 6:53 am
Function to round a decimal to highest possible integer
Hi all,
Is there any function like "Ceil" in datastage that will round up a decimal to highest possible integer??
I am working with 5x and I couldnot find any function like that.
Thank You
Is there any function like "Ceil" in datastage that will round up a decimal to highest possible integer??
I am working with 5x and I couldnot find any function like that.
Thank You
-
- Participant
- Posts: 3337
- Joined: Mon Jan 17, 2005 4:49 am
- Location: United Kingdom
-
- Participant
- Posts: 3337
- Joined: Mon Jan 17, 2005 4:49 am
- Location: United Kingdom
Oops!! Just thought of some pitfalls,
You can define the routine as
Ceil(Arg1)
YourDecimalValue = Arg1
* Avoids if yours is already an int
If Int(YourDecimalValue) = YourDecimalValue Then
CeilValue = YourDecimalValue
End
Else
CeilValue = Int(YourDecimalValue + YourDecimalValue / Abs(YourDecimalValue) )
End
Ans = CeilValue
You can define the routine as
Ceil(Arg1)
YourDecimalValue = Arg1
* Avoids if yours is already an int
If Int(YourDecimalValue) = YourDecimalValue Then
CeilValue = YourDecimalValue
End
Else
CeilValue = Int(YourDecimalValue + YourDecimalValue / Abs(YourDecimalValue) )
End
Ans = CeilValue
-
- Participant
- Posts: 122
- Joined: Mon Jul 05, 2004 1:33 pm
- Location: MA
sad that FIX (number [ ,precision [ ,mode ] ] ) did not exist then ?!
Sainath.Srinivasan wrote:Oops!! Just thought of some pitfalls,
You can define the routine as
Ceil(Arg1)
YourDecimalValue = Arg1
* Avoids if yours is already an int
If Int(YourDecimalValue) = YourDecimalValue Then
CeilValue = YourDecimalValue
End
Else
CeilValue = Int(YourDecimalValue + YourDecimalValue / Abs(YourDecimalValue) )
End
Ans = CeilValue
Regards
Sonia Jacob
Sonia Jacob
-
- Charter Member
- Posts: 143
- Joined: Thu Nov 04, 2004 6:53 am
HeySainath.Srinivasan wrote:Oops!! Just thought of some pitfalls,
You can define the routine as
Ceil(Arg1)
YourDecimalValue = Arg1
* Avoids if yours is already an int
If Int(YourDecimalValue) = YourDecimalValue Then
CeilValue = YourDecimalValue
End
Else
CeilValue = Int(YourDecimalValue + YourDecimalValue / Abs(YourDecimalValue) )
End
Ans = CeilValue
Thank You very much
-
- Participant
- Posts: 54607
- Joined: Wed Oct 23, 2002 10:52 pm
- Location: Sydney, Australia
- Contact:
FIX( number [,precision [,mode]] ) does indeed exist.
Find it in the DataStage BASIC reference guide.
precision is the number of decimal places.
mode is 0 to round, 1 to truncate.
Find it in the DataStage BASIC reference guide.
precision is the number of decimal places.
mode is 0 to round, 1 to truncate.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
-
- Participant
- Posts: 3337
- Joined: Mon Jan 17, 2005 4:49 am
- Location: United Kingdom
Sonia,
We were very fortunate to have the FIX function but were unfortunate that it did not replicate or perform what the ceil function required.
By defn. Ceil 'rounds the value to least highest integer'
whereas FIX can 'round or truncate integer'. You can use it only instead of int function.
So, for 123.45, ceil will return 124, whereas fix can return 123 only.
Vcannadevula,
The routine code provided earlier requires a small change. For negative numbers, the ceil returns integer close to zero than away from it. For e.g., ceil(-123.45) returns -123 and not -124.
So the code needs to be changed as below,
Ceil(Arg1)
YourDecimalValue = Arg1
* Avoids if yours is already an int
If Int(YourDecimalValue) = YourDecimalValue Then
CeilValue = YourDecimalValue
End
Else
CeilValue = Int(YourDecimalValue + 1)
End
Ans = CeilValue
We were very fortunate to have the FIX function but were unfortunate that it did not replicate or perform what the ceil function required.
By defn. Ceil 'rounds the value to least highest integer'
whereas FIX can 'round or truncate integer'. You can use it only instead of int function.
So, for 123.45, ceil will return 124, whereas fix can return 123 only.
Vcannadevula,
The routine code provided earlier requires a small change. For negative numbers, the ceil returns integer close to zero than away from it. For e.g., ceil(-123.45) returns -123 and not -124.
So the code needs to be changed as below,
Ceil(Arg1)
YourDecimalValue = Arg1
* Avoids if yours is already an int
If Int(YourDecimalValue) = YourDecimalValue Then
CeilValue = YourDecimalValue
End
Else
CeilValue = Int(YourDecimalValue + 1)
End
Ans = CeilValue
-
- Participant
- Posts: 54607
- Joined: Wed Oct 23, 2002 10:52 pm
- Location: Sydney, Australia
- Contact:
So you can FIX the next higher number (N + 1) surely?
You might also like to investigate the rounding/truncating capabilities of the FMT function, and the OCONV function with MD conversion specification.
Finally, you could write your own CEIL function as a user-defined Routine.
Make sure that whatever you do handles negative numbers in accordance with your specifications.
You might also like to investigate the rounding/truncating capabilities of the FMT function, and the OCONV function with MD conversion specification.
Finally, you could write your own CEIL function as a user-defined Routine.
Make sure that whatever you do handles negative numbers in accordance with your specifications.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
-
- Participant
- Posts: 3337
- Joined: Mon Jan 17, 2005 4:49 am
- Location: United Kingdom
-
- Participant
- Posts: 54607
- Joined: Wed Oct 23, 2002 10:52 pm
- Location: Sydney, Australia
- Contact:
I don't know why FIX with precision 0 doesn't work for me, but with Fmt it works fine:
Note that unfortunately it won't work for 1.00001.
If You know that Your precision is f.e. 4 then You should add 0.4999 etc.
Code: Select all
Ans = Fmt((Arg1 + 0.49), "0")
If You know that Your precision is f.e. 4 then You should add 0.4999 etc.
Regards,
Wojciech Nogalski
Wojciech Nogalski
-
- Participant
- Posts: 3337
- Joined: Mon Jan 17, 2005 4:49 am
- Location: United Kingdom
-
- Premium Member
- Posts: 1044
- Joined: Wed Sep 29, 2004 3:30 am
- Location: Nottingham, UK
- Contact:
Re: Function to round a decimal to highest possible integer
How about -FIX(-X,0,1)?vcannadevula wrote:Hi all,
Is there any function like "Ceil" in datastage that will round up a decimal to highest possible integer??
I am working with 5x and I couldnot find any function like that.
Thank You
Hm, that doesn't work. I assumed from the replies above that FIX can be used to round to 0 decimal places. It can't. Also, both FIX(x,y,1) and INT go to lower magnitude, so negation won't work either.
Update: GCC implements it this way:
/* Round X to the smallest integer not less then argument, i.e. round
up, placing the result in R in mode MODE. */
do_fix_trunc (r, x);
if (! real_identical (r, x) && ! r->sign)
do_add (r, r, &dconst1, 0);
if (mode != VOIDmode)
real_convert (r, mode, r);
This looks similar to the code suggested by others above, so I'd say that it can't easily be done without an IF.
Last edited by PhilHibbs on Wed Feb 09, 2005 5:23 am, edited 2 times in total.
Phil Hibbs | Capgemini
Technical Consultant
Technical Consultant
-
- Participant
- Posts: 3337
- Joined: Mon Jan 17, 2005 4:49 am
- Location: United Kingdom