× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Hi Griz,

The character field I need to convert always has a decimal followed by 4
numbers. I don't know of any other way to convert an alphanumeric field
and have the 4 decimal point precision.

There are several other ways. The best way if you're running V5R2 or later is to use the %dec() BIF. It will convert from alphanumeric to decimal in one go.


For example:

      ** This code requires V5R2 or later.
     D CharData        s             11A   inz('12.2963    ')
     D decprice        s             11P 4

      /free

         decprice = %dec( CharData : 11: 4);

      /end-free

Prior to V5R2 I'd use Barbara Morris' GETNUM subprocedure. Download it to your iSeries and compile it as a service program, then you can use it from all of your programs.

You can get it from the Midrange.com FAQ:
http://faq.midrange.com/data/cache/52.html

Once you have it installed, you call it as follows:

         decprice = getNum( CharData );

However, the ATOF technique also works. Because you're using floating point, there's always a danger of losing precision -- but it DOES work. The following program compiles and runs flawlessly on my machine.

     H DFTACTGRP(*NO) BNDDIR('QC2LE')

     D atof            PR             8F   extproc('atof')
     D  alpha                          *   value options(*string)

     D CharData        s             11A   inz('12.2963    ')
     D decprice        s             11P 4

      /free

         decprice = %dech( atof(CharData) : 11: 4);
         dsply (%char(decprice));
         *inlr = *on;

      /end-free

That's the whole program (not just a snippet) so you can copy/paste it to your system and compile & run it if you like.

Another thing that occurs to me is that the problem might be in the data rather than in your code. Have you looked at the value of CharData (in hex) before receiving the error? Have you made sure that it's all numbers, the decimal point, and spaces? No unprintable characters? no extra characters at the end of the data?



As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 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.