Page 1 of 1

Format Numbers using sprintf from C library.

Posted: Sun Feb 14, 2010 10:41 pm
by skywalk
I need to format numbers using scientific notation similar to the VB6 format$() function.
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$
EDIT: added Unicode support via 'swprintf()'.

Re: Format Numbers using sprintf from C library.

Posted: Mon Feb 15, 2010 3:26 am
by RASHAD
Thank you for sharing
A little bit modification for PB x64

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
      sPrintf.l(result.s,format.s,num.d) As "_sprintf" 
      sPrintf_LLD.l(result.s,format.s,num1.l,num2.l,num.d) As "_sprintf"
  CompilerElse
      sPrintf.l(result.s,format.s,num.d) As "sprintf" 
      sPrintf_LLD.l(result.s,format.s,num1.l,num2.l,num.d) As "sprintf"
  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,sf
x=-123.456e2
s=Space(32)

sf = "%+e"
sprintf(s,sf,x)
Debug "Using " + sf + " = " + s 

sf="%10.2lf"
sprintf(s,sf,x)
Debug "Using " + sf + " = " + s 

sf="%+*.*lf"    
; use 3 for the width, 4 for the precision and 'x' as the value to format.
sprintf_lld(s, sf, 8, 3, x)
Debug "Using " + sf + " = " + s 

End