Hello,
I have to convert a numeric to a scientific notation , How to ??
Any function ?
10000 ==> 1,E+04
Thanks
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
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
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
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
#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
Return to FiveWin for Harbour/xHarbour
Users browsing this forum: No registered users and 102 guests