Truncating Decimal.

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
highpoint
Premium Member
Premium Member
Posts: 123
Joined: Sat Jun 19, 2010 12:01 am
Location: Chicago

Truncating Decimal.

Post by highpoint »

Hi,

I have stage variable decimal(14,2)
say: 666665555555.12
output needed: 5555555

Input: -666665555555.12
Output: -5555555

I would like to truncate any thing after decimal point and take right 7 digits before decimal. My destination column is Varchar 8.

I am using the following:

Code: Select all

if col<0 then "-":right(abs(col),7)
else right(abs(col),7)
please suggest me the correct way to do this.
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

Does what you're doing work? The only improvement I could suggest would be to use stage variables for clarity and to avoid multiple calculations of the same function. Maybe use Left() function to test for the sign, if the input column data type is string.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
highpoint
Premium Member
Premium Member
Posts: 123
Joined: Sat Jun 19, 2010 12:01 am
Location: Chicago

Post by highpoint »

ray.wurlod wrote:Does what you're doing work? The only improvement I could suggest would be to use stage variables for clarity and to avoid multiple calculations of the same function. Maybe use Left() function to test for the sign, if the input column data type is string.
The problem with the above is it will take values to the right of decimal (precision) which i dont want.
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

That wasn't explicit in your specification.

Code: Select all

svLeftOfDecimal <--  Field(InLink.Col, ".", 1, 1)
svNegative  <--  (Left(svLeftOfDecimal, 1) = "-")
svNumber  <--  Right(svLeftOfDecimal, 7)
svResult  <--  If svNegative Then "-" : svNumber Else svNumber
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
sultan@cts
Participant
Posts: 31
Joined: Thu Jul 05, 2007 11:57 pm
Location: India
Contact:

Re: Truncating Decimal.

Post by sultan@cts »

I am using the following:

Code: Select all

if col<0 then "-":right(abs(col),7)
else right(abs(col),7)
please suggest me the correct way to do this.[/quote]

you can try this code:

Code: Select all

If inlnk.inputfield < 0 Then '-':right(Field(inlnk.inputfield ,'.',1,1),8) Else right(Field(inlnk.inputfield ,'.',1,1),8)
Thanks
Sultan
Sainath.Srinivasan
Participant
Posts: 3337
Joined: Mon Jan 17, 2005 4:49 am
Location: United Kingdom

Post by Sainath.Srinivasan »

Abs will return integer value. So you will loose the decimals.

Use DecimalToString to ensure the conversion. Maybe for +ve numbers you need to prefix with blank space to ensure right padded nature.
PhilHibbs
Premium Member
Premium Member
Posts: 1044
Joined: Wed Sep 29, 2004 3:30 am
Location: Nottingham, UK
Contact:

Post by PhilHibbs »

Sainath.Srinivasan wrote:Abs will return integer value. So you will loose the decimals.
Technically, it returns a dfloat - but the input is an integer, so your trailing decimals get stripped off before they are passed to ABS. How's that for nitpicking? :lol: But, I see you want to strip the trailing decimals anyway, so ABS is ok.
Try this:

Code: Select all

MOD( ABS(value), 10000000 )
Phil Hibbs | Capgemini
Technical Consultant
highpoint
Premium Member
Premium Member
Posts: 123
Joined: Sat Jun 19, 2010 12:01 am
Location: Chicago

Re: Truncating Decimal.

Post by highpoint »

sultan@cts wrote:I am using the following:

Code: Select all

if col<0 then "-":right(abs(col),7)
else right(abs(col),7)
please suggest me the correct way to do this.
you can try this code:

Code: Select all

If inlnk.inputfield < 0 Then '-':right(Field(inlnk.inputfield ,'.',1,1),8) Else right(Field(inlnk.inputfield ,'.',1,1),8)
[/quote]


My target column is varchar. Do i have to do decimal to string conversion or is not needed?
highpoint
Premium Member
Premium Member
Posts: 123
Joined: Sat Jun 19, 2010 12:01 am
Location: Chicago

Post by highpoint »

PhilHibbs wrote:
Sainath.Srinivasan wrote:Abs will return integer value. So you will loose the decimals.
Technically, it returns a dfloat - but the input is an integer, so your trailing decimals get stripped off before they are passed to ABS. How's that for nitpicking? :lol: But, I see you want to strip the trailing decimals anyway, so ABS is ok.
Try this:

Code: Select all

MOD( ABS(value), 10000000 )
Correct me if i am wrong.

The DS Developer manual has this example which shows the precision will not be lost after abs function.

This example uses the Abs function to compute the absolute value of a number.

AbsValue = Abs(12.34) ;* returns 12.34
AbsValue = Abs(-12.34) ;* returns 12.34
arunkumarmm
Participant
Posts: 246
Joined: Mon Jun 30, 2008 3:22 am
Location: New York
Contact:

Post by arunkumarmm »

Did you try it with Field? As Sultan suggested? If not try this

Code: Select all

If inlnk.inputfield < 0 Then '-': (Field(inlnk.inputfield ,'.',1)[7]) Else Field(inlnk.inputfield ,'.',1)[7]
Arun
kumar_s
Charter Member
Charter Member
Posts: 5245
Joined: Thu Jun 16, 2005 11:00 pm

Post by kumar_s »

highpoint wrote: Correct me if i am wrong.

The DS Developer manual has this example which shows the precision will not be lost after abs function.

This example uses the Abs function to compute the absolute value of a number.

AbsValue = Abs(12.34) ;* returns 12.34
AbsValue = Abs(-12.34) ;* returns 12.34
Thats right... AsInteger() might help for this replacement. But need to check the Maximum possible operational byte by this funciton.
Impossible doesn't mean 'it is not possible' actually means... 'NOBODY HAS DONE IT SO FAR'
PhilHibbs
Premium Member
Premium Member
Posts: 1044
Joined: Wed Sep 29, 2004 3:30 am
Location: Nottingham, UK
Contact:

Post by PhilHibbs »

highpoint wrote:Correct me if i am wrong.

The DS Developer manual has this example which shows the precision will not be lost after abs function.

This example uses the Abs function to compute the absolute value of a number.

AbsValue = Abs(12.34) ;* returns 12.34
AbsValue = Abs(-12.34) ;* returns 12.34
That's in the Server Job Developer's Guide, but you've specified that this is in relation to a Parallel Job. Abs() in a Parallel Job converts its input to an Integer. Fabs() works with floating point values in parallel jobs.

Or, are you using a BASIC Transformer? I don't know if they behave like Server in this regard.
Phil Hibbs | Capgemini
Technical Consultant
highpoint
Premium Member
Premium Member
Posts: 123
Joined: Sat Jun 19, 2010 12:01 am
Location: Chicago

Post by highpoint »

PhilHibbs wrote:
highpoint wrote:Correct me if i am wrong.

The DS Developer manual has this example which shows the precision will not be lost after abs function.

This example uses the Abs function to compute the absolute value of a number.

AbsValue = Abs(12.34) ;* returns 12.34
AbsValue = Abs(-12.34) ;* returns 12.34
That's in the Server Job Developer's Guide, but you've specified that this is in relation to a Parallel Job. Abs() in a Parallel Job converts its input to an Integer. Fabs() works with floating point values in parallel jobs.

Or, are you using a BASIC Transformer? I don't know if they behave like Server in this regard.
Thanks, I verified abs function is removing precision for decimals.
sultan@cts
Participant
Posts: 31
Joined: Thu Jul 05, 2007 11:57 pm
Location: India
Contact:

Re: Truncating Decimal.

Post by sultan@cts »

Code: Select all

If inlnk.inputfield < 0 Then '-':right(Field(inlnk.inputfield ,'.',1,1),8) Else right(Field(inlnk.inputfield ,'.',1,1),8)
[/quote]


My target column is varchar. Do i have to do decimal to string conversion or is not needed?[/quote]

Before using the inlnk.inputfield in derivation convert it to decimal to string.
Thanks
Sultan
Post Reply