Focal Point
[SOLVED] Removing Trailing Zeros

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

August 11, 2015, 03:55 PM
GavinL
[SOLVED] Removing Trailing Zeros
We have data we pull from HIVE, which is obviously an ASCII column.. Data is represented as 0.0350000. We do the following, but it leaves trailing zeros 3.500000%, which all we are looking for is 3.5%. However, as I was creating this demo, the below code doesn't seem to work at all, which only adds to my confusion. It always comes back as zero. What am I missing below and how can I remove trailing zeros?


-SET &MYPERC = '0.0350000';
-SET &MYPERC = (ATODBL(&MYPERC, '255', 'D11.6%') * 100);
-TYPE MYPERC = &MYPERC

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



- FOCUS Man, just FOCUS!
-----------------------------
Product: WebFOCUS
Version: 8.1.04
Server: Windows 2008 Server
August 11, 2015, 04:10 PM
Francis Mariani
Do you need to do this in Dialogue manager or WebFOCUS TABLE FILE? There are different solutions depending on where you need to do it.


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
August 11, 2015, 04:12 PM
GavinL
The code we are working with is from a WebFOCUS TABLE FILE..


TABLE FILE AAA/PROC_NAME
PRINT
COMPUTE ASOFDATE/A10 = '&ASOFDATE';
COMPUTE ANNUALINTERESTRATEfmt/A25V = FPRINT(ATODBL(PROC_NAME.RESPONSE.ANNUALINTERESTRATE, '255', 'D11.6%') * 100, 'D11.6%', 'A25V');
...
...




- FOCUS Man, just FOCUS!
-----------------------------
Product: WebFOCUS
Version: 8.1.04
Server: Windows 2008 Server
August 11, 2015, 04:15 PM
Francis Mariani
In Dialogue Manager, you need to set the numeric precision - in this example, it is set to 1 decimal place::

SET DMPRECISION = 1
-RUN

-SET &MYPERC = 0.0350000;
-SET &MYPERC = &MYPERC * 100;
-TYPE MYPERC = &MYPERC


If you need a variable number of decimal places, that might be possible using some other technique.


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
August 11, 2015, 04:18 PM
GavinL
Thanks,

In Dialog Manager, I was trying to represent the ASCII portion, being converted as needed from the TABLE, which is why I wrapped it in single quotes. I'm guessing ATODBL() doesn't work in Dialog Manager.

But WebFOCUS TABLE FILE is where I really need a answer for.



- FOCUS Man, just FOCUS!
-----------------------------
Product: WebFOCUS
Version: 8.1.04
Server: Windows 2008 Server
August 11, 2015, 04:20 PM
David Briars
quote:
how can I remove trailing zeros?

-SET &MYPERC = '0.0350000';
-SET &MYPERC = TRIM('T', &MYPERC, &MYPERC.LENGTH, '0', 1, A&MYPERC.LENGTH); 
-TYPE MYPERC = &MYPERC
-EXIT  

Gives:
MYPERC = 0.035 

August 11, 2015, 04:29 PM
GavinL
I tried the following and got the something closer.. Zeros are gone, but the '.' is at the end..

COMPUTE ANNUALINTERESTRATE2fmt/A25V = TRIM('T', FPRINT(ATODBL(PROC_NAME.RESPONSE.ANNUALINTERESTRATE, '255', 'D11.6%') * 100, 'D11.6%', 'A25V'), 11, '0', 1, 'A255V');


EDIT: Only for the whole %, it shows a decimal on the end. The rest, like 3.75 come out perfectly.

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



- FOCUS Man, just FOCUS!
-----------------------------
Product: WebFOCUS
Version: 8.1.04
Server: Windows 2008 Server
August 11, 2015, 04:39 PM
Francis Mariani
How about not using TRIM, but specifying 'D11.2%'?


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
August 11, 2015, 04:49 PM
GavinL
This is for bank loans and they go to 6 decimals. I only wish.. :P



- FOCUS Man, just FOCUS!
-----------------------------
Product: WebFOCUS
Version: 8.1.04
Server: Windows 2008 Server
August 11, 2015, 05:06 PM
Francis Mariani
The answer is here: [NFR?] Surpress Trailing Zeros


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
August 11, 2015, 05:10 PM
David Briars
Francis, in addition to your excellent IT/Developer skills, you are also a fantastic researcher/searcher. Smiler
August 12, 2015, 05:59 AM
Tony A
In the post to which Francis' refers, there is a suggestion of using a FUNCTION which, as you may use it many times, is an excellent idea here.

As you are looking for a percentage value with the %sign suffixed to the "trimmed" result(?), try this code -
DEFINE FUNCTION REMTRAIL(DBLPREC/D12.6%)
  F_FPRINT/A25 = FPRINT(DBLPREC, 'D12.6%', 'A25');
-* By specifying only 24 chars of output we drop the %sign off
  F_RJUST/A25  = RJUST(25, F_FPRINT, 'A24');
-* Now we force a hard concatenation of the %sign
  REMTRAIL/A25V = TRIM('T', F_RJUST, 24, '0', 1, REMTRAIL)||'%';
END
-RUN

Also, do not forget to SET CENT-ZERO = ON if you want the leading zero if the value is less than 0 e.g. 0.03% instead of .03%

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 
August 12, 2015, 09:17 AM
Francis Mariani
Tony,




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
August 12, 2015, 10:56 AM
Tony A
Thanks Francis, but I was only taking David Briars' code and placing it into a FUNCTION - which we so often forget that we can do.

I remember back in the day when we would have done the unmentionable for an easy method of creating UWS!

T
August 12, 2015, 03:35 PM
Francis Mariani
David, yes I found your excellent solution and thanks to Tony for simply turning it into a function that we should all copy.


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
August 13, 2015, 09:08 AM
GavinL
Sorry, I've been out of pocket, but I'll give the function a try today..

Thanks



- FOCUS Man, just FOCUS!
-----------------------------
Product: WebFOCUS
Version: 8.1.04
Server: Windows 2008 Server
August 13, 2015, 09:18 AM
GavinL
I tried your function, but it seemed to do nothing, but add the % on the end and pad a bunch of spaces to the beginning..

-DEFAULTH &MyVAR = 0.356451;

DEFINE FUNCTION REMTRAIL(DBLPREC/D12.6%)
-* SET CENT-ZERO = ON -*if you want the leading zero if the value is less than 0 e.g. 0.03% instead of .03%
  F_FPRINT/A25 = FPRINT(DBLPREC, 'D12.6%', 'A25');
-* By specifying only 24 chars of output we drop the %sign off
  F_RJUST/A25  = RJUST(25, F_FPRINT, 'A24');
-* Now we force a hard concatenation of the %sign
  REMTRAIL/A25V = TRIM('T', F_RJUST, 24, '0', 1, REMTRAIL)||'%';
END
-RUN

-SET &MyVAR2 = REMTRAIL(&MyVAR);
-TYPE MyVAR2 = &MyVAR2;


Results:
MyVAR2 =                  .356451%;




- FOCUS Man, just FOCUS!
-----------------------------
Product: WebFOCUS
Version: 8.1.04
Server: Windows 2008 Server
August 13, 2015, 09:34 AM
Francis Mariani
Well, there are no trailing zeroes in the Dialogue Manager variable &MyVAR and you stated you need this for TABLE FILE.


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
August 13, 2015, 09:37 AM
Tony A
You beat me to it Francis!

DEFINE FUNCTION REMTRAIL(DBLPREC/D12.6%)
-* SET CENT-ZERO = ON -*if you want the leading zero if the value is less than 0 e.g. 0.03% instead of .03%
  F_FPRINT/A25 = FPRINT(DBLPREC, 'D12.6%', 'A25');
-* By specifying only 24 chars of output we drop the %sign off
  F_RJUST/A25  = RJUST(25, F_FPRINT, 'A24');
-* Now we force a hard concatenation of the %sign
  REMTRAIL/A25V = TRIM('T', F_RJUST, 24, '0', 1, REMTRAIL)||'%';
END
-RUN

DEFINE FILE GGSALES
 D_PERCENT/D12.6 = DECODE SEQ_NO (1     6.21118
                                  2    12.42
                                  3     5.0000000
                                  4     1.01
                                  5 12345.012 ELSE 0);
END
TABLE FILE GGSALES
PRINT SEQ_NO
      D_PERCENT
      COMPUTE C_PERCENT/A25V = REMTRAIL(D_PERCENT);
IF SEQ_NO FROM 1 TO 5
ON TABLE SET ONLINE-FMT STANDARD
END
-EXIT

Giving

  Sequence#      D_PERCENT  C_PERCENT
  ---------      ---------  ---------
          1       6.211180                  6.21118%
          2      12.420000                 12.42%
          3       5.000000                  5.%
          4       1.010000                  1.01%
          5  12,345.012000             12,345.012%


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 
August 13, 2015, 11:20 AM
GavinL
Tony, you still show the same problem I'm already having.. Look at your results. 5.00000 turns into 5.%

I had gotten this to work, but with the same issue.
COMPUTE ANNUALINTERESTRATE2fmt/A25V = TRIM('T', FPRINT(ATODBL(PROC_NAME.RESPONSE.ANNUALINTERESTRATE, '255', 'D11.6%') * 100, 'D11.6%', 'A25V'), 11, '0', 1, 'A255V');



- FOCUS Man, just FOCUS!
-----------------------------
Product: WebFOCUS
Version: 8.1.04
Server: Windows 2008 Server
August 13, 2015, 12:30 PM
Tony A
quote:
Removing Trailing Zeros

Perhaps we should take your question non-literally? Wink

In that case then I would add a section to the function to check if there are any characters after the decimal point and add a zero before the percentage sign.

DEFINE FUNCTION REMTRAIL(DBLPREC/D12.6%)
  F_FPRINT/A25 = FPRINT(DBLPREC, 'D12.6%', 'A25');
-* By specifying only 24 chars of output we drop the %sign off
  F_RJUST/A25V = TRIM('T', RJUST(25, F_FPRINT, 'A24'), 24, '0', 1, F_RJUST);
-* Check for no decimals
  F_DECIMALS/A25V = GETTOK(F_RJUST, 25, -1, '.', 25, 'A25');
-* ... and assign the string to be appended
  F_APPEND/A2 = IF F_DECIMALS NE '' THEN '%' ELSE '0%';
-* Now we force a hard concatenation of the %sign
  REMTRAIL/A25V = F_RJUST||F_APPEND;
END
-RUN

DEFINE FILE GGSALES
 D_PERCENT/D12.6 = DECODE SEQ_NO (1     6.21118
                                  2    12.42
                                  3     5.0000000
                                  4     1.01
                                  5 12345.012 ELSE 0);
END
TABLE FILE GGSALES
PRINT SEQ_NO
      D_PERCENT
      COMPUTE C_PERCENT/A25V = REMTRAIL(D_PERCENT);
IF SEQ_NO FROM 1 TO 5
ON TABLE SET ONLINE-FMT STANDARD
END
-EXIT


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 
August 13, 2015, 03:19 PM
GavinL
Ha.. Well, that's one way, but my requirements are to strip it to 5% or 3.75%. I'll figure it out, this gets me on track..

Thanks



- FOCUS Man, just FOCUS!
-----------------------------
Product: WebFOCUS
Version: 8.1.04
Server: Windows 2008 Server
August 14, 2015, 08:08 AM
George Patton
Good grief. Are people really that picky !! ?? The only way you are going to make this work the way you want is to a) strip out all the trailing zeros and replace them with blanks, convert it all to alpha, then if the last character is a period strip that out too,add a percent sign at the end, figure out the maximum number of characters you will have, pad each value with blanks individually such that tey each have the same length and then right-justify the whole mess.

Or you could just tell your boss that life is too short ...


WebFOCUS 7.7.05 Windows, Linux, DB2, IBM Lotus Notes, Firebird, Lotus Symphony/OpenOffice. Outputs PDF, Excel 2007 (for OpenOffice integration), WP
August 14, 2015, 09:26 AM
GavinL
I don't know if I would call that a picky boss. I would most likely call it a limitation of the Focus language. In most other languages, this is a one liner.



- FOCUS Man, just FOCUS!
-----------------------------
Product: WebFOCUS
Version: 8.1.04
Server: Windows 2008 Server
August 15, 2015, 09:04 AM
George Patton
Smiler Could you show me how it could be done in a one liner in another language, let's say C, or javascript?

Really what it amounts to is that you want it to display as an integer if it is a round number and a floating point if it isn't, with the added complication that the decimal points have to line up and trailing zeros eliminated. That's a lot of work for one line of code ....


WebFOCUS 7.7.05 Windows, Linux, DB2, IBM Lotus Notes, Firebird, Lotus Symphony/OpenOffice. Outputs PDF, Excel 2007 (for OpenOffice integration), WP