Format Numbers using sprintf from C library.

Share your advanced PureBasic knowledge/code with the community.
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Format Numbers using sprintf from C library.

Post 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()'.
Last edited by skywalk on Mon Mar 12, 2012 4:28 pm, edited 2 times in total.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Format Numbers using sprintf from C library.

Post 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
Egypt my love
Post Reply