Focal Point
[CLOSED] Front End Rounding / Rounding Down

This topic can be found at:
https://forums.informationbuilders.com/eve/forums/a/tpc/f/7971057331/m/9337037506

June 20, 2011, 09:38 AM
Charles Richards
[CLOSED] Front End Rounding / Rounding Down
Hello, I wanted to know how to round down or front end rounding.


Round to 2 decimals / hundredths

Ex.

IN
4.365375

OUT
4.36

IN
3478.3924635

OUT
3478.39

IN
234.288345

OUT
234.28

This message has been edited. Last edited by: Kerry,


WebFOCUS 7.6
Windows, All Outputs
June 20, 2011, 10:16 AM
FrankDutch
What I see is not the correct way of rounding.
That would sometimes give little higher output.

waht You can do is first compute the input field by multiplying it with 100 and then make it to an integer. In the nextstep you devide it again by 100;

NEWVALUE1/I9=INPUT*100;
NEWVALUE2/D14.2=NEWVALUE1/100;

this will however not work if the input fields are to large (above 999.999.999)




Frank

prod: WF 7.6.10 platform Windows,
databases: msSQL2000, msSQL2005, RMS, Oracle, Sybase,IE7
test: WF 7.6.10 on the same platform and databases,IE7

June 20, 2011, 10:43 AM
GamP
That's not rounding, that's truncating.
You could use the tandem of converting to alpha (FTOA / PTOA) and back again.


GamP

- Using AS 8.2.01 on Windows 10 - IE11.
in Focus since 1988
June 21, 2011, 09:34 AM
George Patton
Convert to Packed format? /P9.2


WebFOCUS 7.7.05 Windows, Linux, DB2, IBM Lotus Notes, Firebird, Lotus Symphony/OpenOffice. Outputs PDF, Excel 2007 (for OpenOffice integration), WP
June 21, 2011, 10:09 AM
jimster06
Consider using half adjust and truncate routine; in this case add .005 to positive numbers and -0.005 to negative numbers and then truncate to two decimal places.


jimster06
DevStu WF 7.6.11
W7
HTML, PDF, EXL2K
June 21, 2011, 10:22 AM
FrankDutch
According to the first example he wants to truncate.

But if you want to round you can do the same but first add 0.005 to the original value.




Frank

prod: WF 7.6.10 platform Windows,
databases: msSQL2000, msSQL2005, RMS, Oracle, Sybase,IE7
test: WF 7.6.10 on the same platform and databases,IE7

June 21, 2011, 10:27 AM
Francis Mariani
Frank's suggestion is the best way to deal with this truncation:

-SET &ECHO='ALL';

FILEDEF file1 DISK file1.txt
-RUN

-WRITE FILE1 4.365375
-WRITE FILE1 3478.3924635
-WRITE FILE1 234.288345

FILEDEF MASTER DISK file1.mas
-RUN

-WRITE MASTER FILENAME=FILE1, SUFFIX=FIX
-WRITE MASTER SEGNAME=FILE1
-WRITE MASTER FIELDNAME=AMT1, ALIAS=AMT1, FORMAT=P12.7, ACTUAL=A12, $

TABLE FILE FILE1
PRINT
AMT1
COMPUTE AMT1A/D12.2 = AMT1;
COMPUTE AMT1B/P12.2 = AMT1;
COMPUTE AMT1B/F12.2 = AMT1;

COMPUTE AMT1C1/I8 = AMT1 * 100;
COMPUTE AMT1C2/P10.2 = AMT1C1 / 100;
END
-RUN



Francis


Give me code, or give me retirement. In FOCUS since 1991

Production: WF 7.7.05M, Dev Studio, BID, MRE, WebSphere, DB2 / Test: WF 8.1.05M, App Studio, BI Portal, Report Caster, jQuery, HighCharts, Apache Tomcat, MS SQL Server
June 21, 2011, 02:04 PM
FrankDutch
I was looking to create a function for this.
I hope somebody has done that though.

DEFINE FUNCTION ROUND(BASEVALUE,NUMBERS)

To get this dynamical with the field NUMBERS as the variable.




Frank

prod: WF 7.6.10 platform Windows,
databases: msSQL2000, msSQL2005, RMS, Oracle, Sybase,IE7
test: WF 7.6.10 on the same platform and databases,IE7

June 21, 2011, 05:23 PM
Tony A
Oh Frank,

You know I can never resist a challenge, however small Smiler

A small beginning for you -
-* Rounding function
-*      Basevalue : value to be rounded;
-*      Direction : round up or round down;
-*      Decimals  : number of decimal places to round to;
DEFINE FUNCTION ROUND_IT(BASEVALUE/D20.6, DIRECTION/A4, DECIMALS/I2)
  ADJ_BASE/D12.8  = 5 / EXP((LOG(10) * (DECIMALS + 1)), 'D12.8');
  ADJUSTER/D12.8  = IF DIRECTION EQ 'UP' THEN ADJ_BASE ELSE 0 - ADJ_BASE;
  ROUND_IT/D20.8  = BASEVALUE + ADJUSTER;
END

FILEDEF file1 DISK file1.txt
-RUN
-WRITE FILE1 4.365375
-WRITE FILE1 3478.3924635
-WRITE FILE1 234.288345
FILEDEF MASTER DISK file1.mas
-RUN
-WRITE MASTER FILENAME=FILE1, SUFFIX=FIX
-WRITE MASTER SEGNAME=FILE1
-WRITE MASTER FIELDNAME=AMT, ALIAS=AMT, FORMAT=P12.7, ACTUAL=A12, $

TABLE FILE FILE1
PRINT AMT
      COMPUTE AMT1/D12.2 = ROUND_IT(AMT, 'UP',  2);
      COMPUTE AMT2/D12.2 = ROUND_IT(AMT, 'DOWN', 2);
      COMPUTE AMT3/P12.2 = ROUND_IT(AMT, 'UP',  2);
      COMPUTE AMT4/P12.2 = ROUND_IT(AMT, 'DOWN', 2);
END

Thanks for your base code Francis, saved a bit of time Smiler

T



In FOCUS
since 1986
WebFOCUS Server 8.2.01M, thru 8.2.07 on Windows Svr 2008 R2  
WebFOCUS App Studio 8.2.06 standalone on Windows 10 
June 21, 2011, 06:01 PM
Francis Mariani
Tony, I'm glad you lost your resistance before me! Nice job.


Francis


Give me code, or give me retirement. In FOCUS since 1991

Production: WF 7.7.05M, Dev Studio, BID, MRE, WebSphere, DB2 / Test: WF 8.1.05M, App Studio, BI Portal, Report Caster, jQuery, HighCharts, Apache Tomcat, MS SQL Server
June 22, 2011, 01:45 AM
FrankDutch
I knew somebody would do it.

thanks a lot on behalf of all the focalpoint users




Frank

prod: WF 7.6.10 platform Windows,
databases: msSQL2000, msSQL2005, RMS, Oracle, Sybase,IE7
test: WF 7.6.10 on the same platform and databases,IE7

June 23, 2011, 05:25 PM
Tony A
Thanks Guys,

I thought that I ought to mention that the use of D12.8 "restricts" the decimal places to 7 which should be good for most apps. Well that's the theory of course.

If anyone is puzzling over what the "5 / EXP((LOG(10) * (DECIMALS + 1))" achieves -
LOG(10) = gives the natural log of 10
multiplying this by the number of decimal places required and then inversing the natural log (using EXP) give values of 10 for decimals = 0, 100 for decimals = 1, 1000 for decimals = 2 etc.
by dividing 5 by this value we achieve respective values of .5, .05 and .005.
Up or Down will negate this value as necessary before adding it to the original number to give your "rounded" result.

Things to note are -
no validation is actioned upon the inputs into the function,
nor is the output really rounded as you might think (double precision at work!)

So use with a little understanding and care.

Yet another one to put into the FUNCTION bank.

T



In FOCUS
since 1986
WebFOCUS Server 8.2.01M, thru 8.2.07 on Windows Svr 2008 R2  
WebFOCUS App Studio 8.2.06 standalone on Windows 10 
June 29, 2011, 03:49 AM
FrankDutch
here is an other solution for the rounding function

-* File functions.fex
-* Rounding function
-*      Basevalue : value to be rounded;
-*      Direction : round up or round down;
-*      Decimals  : number of decimal places to round to;
DEFINE FUNCTION ROUND_IT(BASEVALUE/D20.6, DIRECTION/A4, DECIMALS/I2)
  CORRECTOR/D7.0= 10**(-DECIMALS);
-* raising 10 to the power of the number of decimals
  ADJ_BASE/D12.8= CORRECTOR/2;
-* to calculate the amount you want to add or subtract
  ADJUSTER/D12.8  = IF DIRECTION EQ 'UP' THEN ADJ_BASE ELSE 0 - ADJ_BASE;
  NEWBASE/D20.8= IF BASEVALUE LT 0 THEN BASEVALUE - ADJUSTER ELSE BASEVALUE+ADJUSTER ;
-* newbase is the adjusted base for rounding
  ROUND_ON/D20.8= INT(NEWBASE/CORRECTOR);
-* devide the newbase with the corrector pe 3478.3964 + 0.005 with 10^-2 gives 347840.14 integer is 347840
  ROUND_IT/D20.8=ROUND_ON*CORRECTOR;
-* integer multiplied with the 10^-2 is 3478.40
END

FILEDEF file1 DISK file1.txt
-RUN
-WRITE FILE1 4.365375
-WRITE FILE1 3478.3964635
-WRITE FILE1 -475.7924311
-WRITE FILE1 234.288345
-WRITE FILE1 -234.288345
FILEDEF MASTER DISK file1.mas
-RUN
-WRITE MASTER FILENAME=FILE1, SUFFIX=FIX
-WRITE MASTER SEGNAME=FILE1
-WRITE MASTER FIELDNAME=AMT, ALIAS=AMT, FORMAT=P12.7, ACTUAL=A12, $

TABLE FILE FILE1
PRINT AMT
      COMPUTE AMT0/D12.5 = ROUND_IT(AMT, 'UP',  4);
      COMPUTE AMT1/D12.5 = ROUND_IT(AMT, 'UP',  3);
      COMPUTE AMT2/D12.5 = ROUND_IT(AMT, 'UP', 2);
      COMPUTE AMT3/P12.5 = ROUND_IT(AMT, 'UP',  1);
      COMPUTE AMT0/P12.5 = ROUND_IT(AMT, 'UP', 0);
      COMPUTE AMT4/P12.5 = ROUND_IT(AMT, 'UP', -1);
	  COMPUTE AMT5/P12.5= ROUND_IT(AMT, 'UP',  -2);
	  COMPUTE AMT6/P12.5 = ROUND_IT(AMT, 'UP',  -3);
END


This one does the same as the round function in Excel, in fact the DIRECTION (UP or DOWN) is an strange part in it.
Rounding is always up if the figure is 5 or higher.

The difference with Tony's solution is that in this case the hidden result is also as it should be.




Frank

prod: WF 7.6.10 platform Windows,
databases: msSQL2000, msSQL2005, RMS, Oracle, Sybase,IE7
test: WF 7.6.10 on the same platform and databases,IE7