Focal Point
[TECHNIQUE] Converting To Alpha

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

July 17, 2012, 02:55 PM
Kerry
[TECHNIQUE] Converting To Alpha
Converting To Alpha
by Art Greenhaus

The FOCUS and WebFOCUS language supports a multitude of data formats: alpha, integer, single precision floating point (F), double precision floating point (D), packed (P), smart date, date-time and text. The numeric formats all have different uses, and it is fairly easy to convert them to other formats.

One conversion that is frequently needed is conversion to alpha. Why? Well, if you want to do any character manipulation with a field (stripping out or altering characters, appending to other strings or breaking into components for re-arrangement, to name a few), alpha strings are easier to manipulate.

Traditionally, users have used routines such as EDIT to convert a number to a character string. Unfortunately, EDIT poses certain limitations. For one, it only works with INTEGERS (How would it know how many decimal places to create?). It also adds leading zeros, so the resultant string is always the output length desired (an I6 field containing 123 would become '000123'). In addition, on some platforms you're limited to INTEGER magnitude (how much can be stored in an I4 internal format).

Click here to read more, and subscribe today to receive the WebFOCUS Newsletter in email.
July 17, 2012, 04:57 PM
Francis Mariani
This one of the best things to come aling in a long time.

The other is -READFILE.


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
July 18, 2012, 10:14 AM
Mighty Max
Looks like this is a 7.7.x new feature. http://documentation.informati.../source/topic113.htm. I appears to be working fine in my 7.6.10 localhost. Interesting.


WebFOCUS 8.1.05M Unix Self-Service/MRE/Report Caster - Outputs Excel, PDF, HTML, Flat Files
July 18, 2012, 11:44 AM
DavSmith
Agreed Francis. Will be so great to use FPRINT instead of whipping out my subroutine manual everytime I need to convert a value.

Max, you're right! FPRINT works in our 7.6.11 environment. Here is another take on Art's code that hilights replacing 5 separate numeric to alpha conversion routines with FPRINT.

DEFINE FILE CAR
-*
-* Set initial values
-*
  IVAL                /I6     = 99.99;
  FVAL                /F6.2   = 99.99;
  PVAL                /P5.2   = 99.99;
  YYMD_DM             /YYMD   = &YYMD;
  HYYMDS_HGETC        /HYYMDS = HGETC(8,'HYYMDS');
-*
-* Use OLD conversion routines to convert numberic values defined above to alpha
-* 
  IVAL_EDIT           /A6     = EDIT(IVAL);
  FVAL_FTOA           /A6     = FTOA(FVAL, '(F6.2)', 'A6');
  PVAL_PTOA           /A5     = PTOA(PVAL, '(P5.2)', 'A5');
  YYMD_DM_DATECVT     /A10    = EDIT(DATECVT(&YYMD,'I8YYMD','A8YYMD'),'9999/99/99');
  HYYMDS_HGETC_HCNVRT /A19    = HCNVRT(HGETC(8,'HYYMDS'),'(HYYMDS)',19,'A19');
-*
-* Use FPRINT function to convert numeric values defined above to alpha
-*
  IVAL_FPRINT         /A6     = FPRINT(IVAL,'I6','A6');
  FVAL_FPRINT         /A6     = FPRINT(FVAL,'F6.2','A6');
  PVAL_FPRINT         /A5     = FPRINT(PVAL,'P5.2','A5');
  YYMD_DM_FPRINT      /A10    = FPRINT(&YYMD,'I8YYMD','A10');
  HYYMDS_HGETC_FPRINT /A19    = FPRINT(HGETC(8,'HYYMDS'),'HYYMDS','A19');
END

TABLE FILE CAR
HEADING
"Numeric Value <+0> Old Conversion <+0> FPRINT Conversion"
PRINT IVAL         IVAL_EDIT           AS EDIT    IVAL_FPRINT         AS FPRINT OVER
      FVAL         FVAL_FTOA           AS FTOA    FVAL_FPRINT         AS FPRINT OVER
      PVAL         PVAL_PTOA           AS PTOA    PVAL_FPRINT         AS FPRINT OVER
      YYMD_DM      YYMD_DM_DATECVT     AS DATECVT YYMD_DM_FPRINT      AS FPRINT OVER
      HYYMDS_HGETC HYYMDS_HGETC_HCNVRT AS HCNVRT  HYYMDS_HGETC_FPRINT AS FPRINT     
WHERE COUNTRY EQ 'ENGLAND'
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
FONT='ARIAL', SIZE=9,$
TYPE=HEADING,HEADALIGN=BODY,STYLE=BOLD,SIZE=10,$
TYPE=HEADING,ITEM=1,COLSPAN=2,JUSTIFY=CENTER,BACKCOLOR='Pale Green',$
TYPE=HEADING,ITEM=2,COLSPAN=2,JUSTIFY=CENTER,BACKCOLOR='Yellow',$
TYPE=HEADING,ITEM=3,COLSPAN=2,JUSTIFY=CENTER,BACKCOLOR='Light Blue',$
END


Thanks Kerry.



In FOCUS since 1985 - WF 8.009/8.104 Win 8 Outputs: ALL of 'em! Adapters: Sql Server Teradata Oracle