Convert Alpha to Numeric
Last Post 18 Jun 2002 07:46 AM by Anonymous. 10 Replies.
AddThis - Bookmarking and Sharing Button Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages Not Resolved
Anonymous
Editorial Staff Member
Editorial Staff Member
Posts:81236

--
18 Jun 2002 07:01 AM
Does anyone have some RPG code that would convert an alpha field to a numeric field? I have been using an RPG II routine with arrays for many years but now that I need to increase the size of the field, I need to change this cumbersome code. The alpha field would have values left justified as follows: 12345 -12345 123.45 12.345 Thanks for any help.
Anonymous
Editorial Staff Member
Editorial Staff Member
Posts:81236

--
18 Jun 2002 07:46 AM Accepted Answer
Sure, just use the %DEC() built-in function. You can specify a character value, and %DEC() will return the value in decimal numeric format. But if you want an answer that will work on a release prior to V5R2, you can find lots of answers if you use the search button on this web site.
Anonymous
Editorial Staff Member
Editorial Staff Member
Posts:81236

--
18 Jun 2002 07:54 AM Accepted Answer
Hans - That bordered on being sadistic :-) (The sadist words we hear from you Are simply these - V5R2) Bill
Anonymous
Editorial Staff Member
Editorial Staff Member
Posts:81236

--
19 Jun 2002 06:53 AM Accepted Answer
Hi Hans, Finally... a %DEC function ... anyway, just tested it at V5R2 and it seems to work fine (however a bit to flexible if you ask me). Guess I'll keep my own wrapper function, and maybe make it a bit easier via the %DEC function. Now, let's wait till everybody is at V5R2...
Anonymous
Editorial Staff Member
Editorial Staff Member
Posts:81236

--
19 Jun 2002 07:21 AM Accepted Answer
I am fond of using the c functions atof and atoi. here is a sample and the prototypes. (this will work for rpg iv) If you need to use in RPGIII create an rpg iv program and call it from rpg III D atof PR 8F Extproc('atof') D dValue * VALUE Options(*STRING) D atoi PR 10i 0 Extproc('atoi') D iValue * VALUE Options(*STRING) In the calc section you would do this C Eval NumberField = atof(123.45) You will need to bind to QC2LE binding dir to take advantage of this. I wrote a program that parsed char data received from a remote system and used both of these functions extensivly. they are very fast and reliable. no more decimal data errors. I can provide full working source if you like. email me @ jhaag@kraft.com
Anonymous
Editorial Staff Member
Editorial Staff Member
Posts:81236

--
19 Jun 2002 07:34 AM Accepted Answer
Check club tech Iseries pgm tips by Gary Guthrie, Issue 61 January 31,2002. He has the answer with the code and explaination
Anonymous
Editorial Staff Member
Editorial Staff Member
Posts:81236

--
19 Jun 2002 07:37 AM Accepted Answer
Jason, I guess you haven't read all the posts about the inaccurracies of floating point numbers and why atof shouldn't be used in business applications. Search this forum for atof and author Barbara Morris for some enlightening reading, you will also find a procedure that can be used instead of atof. Scott Mildenberger
Anonymous
Editorial Staff Member
Editorial Staff Member
Posts:81236

--
19 Jun 2002 07:47 AM Accepted Answer
Be careful when using atof() to convert a string to decimal numeric. Generally, you need to use half-adjust in the assignment for it to work properly. atof() returns a value in float numeric format, and since float cannot precisely represent many decimal values, often you get a value that's off by one on the least significant digit. For example, atof('0.03') will return a float value of 0.029999999999. When assigned to a two decimal place decimal variable, you then get the value 0.02. This is normal behavior for float values, and is perfectly acceptable for most applications that typically use float. (Of course, previous discussion on this subject have pointed this out.)
Anonymous
Editorial Staff Member
Editorial Staff Member
Posts:81236

--
19 Jun 2002 07:48 AM Accepted Answer
> Jason,
>
> I guess you haven't read all the posts
> about the inaccurracies of floating point numbers
> and why atof shouldn't be used in business
> applications. Search this forum for atof and
> author Barbara Morris for some enlightening
> reading, you will also find a procedure that can
> be used instead of atof.
>
> Scott Mildenberger

Anonymous
Editorial Staff Member
Editorial Staff Member
Posts:81236

--
19 Jun 2002 07:50 AM Accepted Answer
I wouldn't say "don't use float". I think use of float is acceptable IF you know what you're doing. Generally, when assigning float values to decimal, integer, or unsigned, you need to use half-adjust.
Anonymous
Editorial Staff Member
Editorial Staff Member
Posts:81236

--
20 Jun 2002 05:33 AM Accepted Answer
I seem to recall using a half adjust when i use floating point. Thanks for all the reminders :).
You are not authorized to post a reply.

Acceptable Use Policy