Format$(123.456e5, " ##0.0##e+##;-##0.0##e+##; ##0.0##e+##")
Instead of writing that algorithm(ouch), I compromised with a call to the sprintf() C function.
Would be great if this was native in PureBasic.
Code: Select all
ImportC "" ; "MSVCRT.LIB"
; int sprintf( char *buffer, const char *format, ... )
; Output is sent to buffer. Return value is number of characters written.
; Since variable arguments, must declare a specific function for each combination required.
CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
CompilerIf #PB_Compiler_Unicode
sPrintf.l(result$,format$,num.d) As "_swprintf"
sPrintf_LLD.l(result$,format$,num1.l,num2.l,num.d) As "_swprintf"
CompilerElse
sPrintf.l(result$,format$,num.d) As "_sprintf"
sPrintf_LLD.l(result$,format$,num1.l,num2.l,num.d) As "_sprintf"
CompilerEndIf
CompilerElse
CompilerIf #PB_Compiler_Unicode
sPrintf.l(result$,format$,num.d) As "swprintf"
sPrintf_LLD.l(result$,format$,num1.l,num2.l,num.d) As "swprintf"
CompilerElse
sPrintf.l(result$,format$,num.d) As "sprintf"
sPrintf_LLD.l(result$,format$,num1.l,num2.l,num.d) As "sprintf"
CompilerEndIf
CompilerEndIf
EndImport ;/* Print a floating-point number in engineering notation */
; Format Specifier Type
; %d (Or %i) int
; %c char
; %f float
; %lf double
; %s string
; %x hexadecimal
Define.d x
Define.l rl
Define.s s$,f$
x = -123.456e2
s$ = Space(32)
f$ = "%+e"
sprintf(s$,f$,x)
Debug "Using " + f$ + " = " + s$
f$ = "%10.2lf"
sprintf(s$,f$,x)
Debug "Using " + f$ + " = " + s$
f$ = "%+*.*lf"
; use 3 for the width, 4 for the precision and 'x' as the value to format.
sprintf_lld(s$, f$, 8, 3, x)
Debug "Using " + f$ + " = " + s$