Convert Real to Scientific Format

Convert Real to Scientific Format

Postby Jack » Wed Jun 22, 2011 7:57 pm

Hello,

I have to convert a numeric to a scientific notation , How to ??

Any function ?

10000 ==> 1,E+04

Thanks
Jack
 
Posts: 288
Joined: Wed Jul 11, 2007 11:06 am

Re: Convert Real to Scientific Format

Postby nageswaragunupudi » Thu Jun 23, 2011 2:04 am

Method-1
Code: Select all  Expand view
function N2E( nNum, nDec )

   local e := 0, cRet

   DEFAULT nDec := 2

   if nNum >= 10
      do while nNum >= 10
         nNum  /= 10
         e++
      enddo
   elseif nNum < 1
      do while nNum < 1
         nNum  *= 10
         e--
      enddo
   endif
   cRet  := Str( nNum, nDec + 2, nDec ) + " E " + If( e >= 0, "+", "" ) + LTrim( Str( e ) )

return cRet
 

Method-2
Code: Select all  Expand view
function N2E( nNum, nDec )

   local e := 0, cRet

   DEFAULT nDec := 2

   e     := Floor( Log10( nNum ) )
   nNum  *= 10 ^ ( -e )

   cRet  := Str( nNum, nDec + 2, nDec ) + " E " + If( e >= 0, "+", "" ) + LTrim( Str( e ) )

return cRet
 

I personally like the approach in the second function. But I am not sure which is faster.

Also
Code: Select all  Expand view
function N2E( nNum, nDec )

   local e := 0, nLog, cRet

   DEFAULT nDec := 2

   nLog  := Log10( nNum )
   e     := Floor( nLog )
   nNum  := 10 ^ ( nLog - e )
   cRet  := Str( nNum, nDec + 2, nDec ) + " E " + If( e >= 0, "+", "" ) + LTrim( Str( e ) )

return cRet
 
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10631
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Convert Real to Scientific Format

Postby nageswaragunupudi » Thu Jun 23, 2011 4:46 am

One more way:
Code: Select all  Expand view
function N2SCI( nNum, nDec )

   local e  := 0
   local nAt, cRet

   DEFAULT nDec := 2

   cRet     := LTrim( Str( nNum ) )
   if nNum >= 10
      nAt   := At( ".", cRet + "." )
      e     := nAt - 2
      cRet  := Left( cRet, 1 ) + "." + SubStr( cRet, 2, nAt - 2 ) + SubStr( cRet, nAt + 1 )
      cRet  := LTrim( Str( Val( cRet ), nDec + 2, nDec ) )
      cRet  += " E +" + LTrim( Str( e ) )
   elseif nNum < 1
      nAt   := 3
      do while SubStr( cRet, nAt, 1 ) == '0'
         nAt++
      enddo
      e     := nAt - 2
      cRet  := SubStr( cRet, nAt, 1 ) + "." + SubStr( cRet, nAt + 1 )
      cRet  := LTrim( Str( Val( cRet ), nDec + 2, nDec ) )
      cRet  += " E -" + LTrim( Str( e ) )
   else
      cRet  := Str( nNum, nDec + 2, nDec ) + " E +0"
   endif

return cRet
 

We can compare performance of the above functions and choose the fastest.
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10631
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Convert Real to Scientific Format

Postby Enrico Maria Giordano » Thu Jun 23, 2011 12:25 pm

Try also this sample using C language:

Code: Select all  Expand view
#include "Fivewin.ch"


FUNCTION MAIN()

    ? N2E( 10000 )

    RETURN NIL



#pragma BEGINDUMP

#include "hbapi.h"


HB_FUNC( N2E )
{
    char Buf[ 100 ];

    sprintf( Buf, "%E", hb_parnd( 1 ) );

    hb_retc( Buf );
}

#pragma ENDDUMP


EMG
User avatar
Enrico Maria Giordano
 
Posts: 8712
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Convert Real to Scientific Format

Postby nageswaragunupudi » Thu Jun 23, 2011 12:28 pm

Mr Enrico

Excellent idea !!!
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10631
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Convert Real to Scientific Format

Postby Jack » Thu Jun 23, 2011 3:00 pm

Thanks for this feed back .
Jack
 
Posts: 288
Joined: Wed Jul 11, 2007 11:06 am


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 102 guests