|
Hi Chris, > I need to know how to convert a string to a numeric. The following works > fine. While it may seem to work okay for you, there are a number of problems with it that I'll point out below. > > d thedate s d > d startdate s 7a > > > /free > thedate = %date(*date); > > startdate = '1' + %subst(%char(thedate):3:2) + > %subst(%char(thedate):6:2) + %subst(%char(thedate):9:2); > /end-free > c seton lr Problems: a) you're using the job date rather than the system date. If you sign on at 11:00pm, do other things, then run this program after midnight, the date that you get will be wrong. 99% of the time, people don't want to use *DATE. You can call %date() without any parameters to get the current system date. b) You're hard-coding the century field as '1', which will cause your program to not work properly for dates earlier than 2000 or later than 2099. Please don't create another Y2K nightmare!! It's unnecessary, the %char() BIF will do it for you if you specify *CYMD c) All of the %subst() code is not necessary, the %char() bif can do that for you as well, just add a 0 to the end of the date format, like *CYMD0 d) If you're intending to write a free-format program, please don't switch back to fixed-format just to set on the LR indicator. That looks silly! You can just do *inlr = *on > startdate = %ConvertThisSuckerToaNumber('1' + > %subst(%char(thedate):3:2) + > %subst(%char(thedate):6:2) + %subst(%char(thedate):9:2)); Ahh yes, the %ConvertThisSuckerToaNumber() BIF. I remember it well. In V5R2 and later, the %dec() and %int() BIFs can convert to a number. I suspect that you're working in V5R1, and that's why you're not using them? In V5R1, one easy workaround is to call the atoi() function from ILE C. Here's how I'd write your program in V5R1: H DFTACTGRP(*NO) BNDDIR('QC2LE') D atoi pr 10I 0 extproc('atoi') D string * value options(*string) D numdate s 7P 0 /free numdate = atoi( %char( %date() : *CYMD0 ) ); *inlr = *on; /end-free In V5R2 I can use %dec() instead: D numdate s 7P 0 /free numdate = %dec( %char( %date() : *CYMD0 ) : 7 : 0 ); *inlr = *on; /end-free Hope you are learning a lot :)
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2025 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.