• Show large number as correct scientific

    From Jack Konings@21:1/5 to All on Wed Jan 27 23:40:44 2021
    Hi,

    It's been a long time since i visited this group. Glad to see its still around.

    My problem is the following. Somewhere in the code a large number is read in from a text file and converted to a float like f_number:=val('5.1E+17').
    Further downstream this value needs to be presented as a string in a scientific notation again, 51.00E+16.
    I experimented with the setscience() setting and multiple str3() and ntrim()'s but can't get i right. I always wind up with something like 5.1000000000000000e+17 (didn't count the zeros) or ****************.
    I would expect decimal and exponent formatting like str3(f_number, 9, 2) result in '51.00E+16'.

    What am I doing wrong?

    TIA,

    Jack

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Armin Brunwinkel@21:1/5 to All on Fri Jan 29 02:41:57 2021
    Hi Jack,

    just 'quick and dirty' :


    LOCAL cDigit AS STRING
    LOCAL cScience AS STRING
    LOCAL lsnsetting AS LOGIC
    LOCAL nValue AS FLOAT
    LOCAL nFloat AS FLOAT
    LOCAL nFactor AS SHORTINT
    LOCAL nPOW AS DWORD

    ? 'See the problem...'

    ? POW( 10 , 9 ) , ' Working...'
    ? POW( 10 , 10 ) , ' ...not'
    ? Transform( POW( 10 , 10 ) , '99999999999' ) , ' Working'
    ? Transform( POW( 10 , 11 ) , '999999999999' ) , ' Working'
    ? Transform( POW( 10 , 12 ) , '9999999999999' ) , ' Working,...'
    ? '-----------------------------'
    ? nValue := 1234567890.00 , ' Working example for SetScience() (see VO Help)...'
    lsnsetting := SetScience()
    SetScience(TRUE)
    ? nValue
    SetScience(FALSE)
    ? nValue
    SetScience(lsnsetting )
    ? nValue := 12345678901.00 , ' ..not working example for SetScience())' lsnsetting := SetScience()
    SetScience(TRUE)
    ? nValue
    SetScience(FALSE)
    ? nValue
    SetScience(lsnsetting )
    ? '-----------------------------'
    ? 'Your example 5.1E+17 ( or 5.1E-17 )'

    cScience := '5.1E+17' // Or '5.1E-17'

    ' A kind of fracking...'

    ? nValue := Val( Left( cScience , At( 'E' , cScience ) - 1 ) )

    IF At( '-' , cScience ) > 0

    ? nFactor := -1

    ELSE

    ? nFactor := 1

    ENDIF

    ? nPOW := Val( StrTran( StrTran( SubStr( cScience , At( 'E' , cScience ) + 1 ) , '+' , '' ) , '-' , '' ) )
    ? Transform( POW( 10 , nPOW ) , Replicate( '9' , nPOW + 1 ) ) , ' Working as seen above'
    ? nValue * POW( 10 , 3 ) , ' Working...'
    ? nValue * POW( 10 , 4 )
    ? nValue * POW( 10 , 5 )
    ? nValue * POW( 10 , 6 )
    ? nValue * POW( 10 , 7 )
    ? nValue * POW( 10 , 8 )
    ? nValue * POW( 10 , 9 )
    ? nValue * POW( 10 , 10 ) , ' .. not'
    nFloat := Round( nValue , 1 ) * POW( 10 , 10 )
    ? Transform( nFloat , Replicate( '9' , nPOW + 1 ) ) , ' Working as seen above'

    ? 'At least ..'

    IF nFactor < 0

    nFloat := Round( -1* nValue * 10 , 0 )
    nFloat := nFloat * POW( 10 , nPOW - 1 )

    ? cDigit := Transform( nFloat , Replicate( '9' , nPOW + 2 ) )
    ? '...and backwards to scientific...'
    ? cScience := SubStr( cDigit , 2 , 1 ) + "." + SubStr( cDigit , 3 , 1 ) + 'E-' + LTrim( Str( Len( SubStr( cDigit , 4 ) ) + 1 , 3 ) )

    ELSE

    nFloat := Round( nValue * 10 , 0 )
    nFloat := nFloat * POW( 10 , nPOW - 1 )

    ? cDigit := Transform( nFloat , Replicate( '9' , nPOW + 1 ) )
    ? '...and backwards to scientific...'
    ? cScience := SubStr( cDigit , 1 , 1 ) + "." + SubStr( cDigit , 2 , 1 ) + 'E+' + LTrim( Str( Len( SubStr( cDigit , 3 ) ) + 1 , 3 ) )

    ENDIF


    Armin

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)