How to handle Scientific Notation

Cecil

19+ years progress programming and still learning.
Hi

I'm importing a values from an excel/csv spreadsheet and the decimal input value is 1.0999999999999999E-2 .

Has anyone developed a function which can handle the Scientific Notation where the input is 1.0999999999999999E-2 . and the output is 0.010999999999999999 ......

Unfortunatly the ABL 'decimal' function can't handle the Scientific Notation.

P.S. I am also aware the ABL's decimal data type can only store a maxium of ten decimal places.
 

Cecil

19+ years progress programming and still learning.
I need a code review on this please. Can it be made better. Thanks

Code:
FUNCTION NormaliseNotation RETURNS CHARACTER 
    (INPUT pchNumber AS CHARACTER):
    DEFINE VARIABLE deNotation  AS DECIMAL     NO-UNDO DECIMALS 10.
    DEFINE VARIABLE inPower     AS INTEGER     NO-UNDO .
    
    IF pchNumber MATCHES '*E*':U THEN
    DO:
        ASSIGN
            deNotation = DECIMAL(SUBSTRING(pchNumber, 1,R-INDEX(pchNumber,'E') - 1))
            inPower = INTEGER(SUBSTRING(pchNumber, R-INDEX(pchNumber,'E') + 1)).
            
        MESSAGE deNotation SKIP inPower SUBSTRING(pchNumber, 1,R-INDEX(pchNumber,'E') - 1).
        RETURN STRING(deNotation * EXP(10,inPower)).
    END.
    ELSE
        RETURN pchNumber.    
END FUNCTION.
MESSAGE  NormaliseNotation('8.9999999999999993E-3').
 

mosfin

Member
1) i think you should add NO-ERROR to your ASSIGN statement,
and after check for ERROR-STATUS:ERROR, to catch invalid data
 

Stefan

Well-Known Member
I prefer NUM-ENTRIES and ENTRY to pick strings apart.

Code:
IF NUM-ENTRIES( pchNumber, 'E':U ) = 2 THEN DO:

Resulting in a much cleaner assign:

Code:
ASSIGN
   de = DECIMAL( ENTRY( 1, pchNumber, 'E':U ) )
   ip = INTEGER( ENTRY( 2, pchNumber, 'E':U ) )
   .
 
Top