Number Formating function

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
surefire00
New User
New User
Posts: 4
Joined: Thu May 11, 2006 2:26 pm
Location: UK

Number Formating function

Post by surefire00 »

I might have missed something, but I can not fund a number formating function such as

FORMATING$("Amount Due","###.0#",x)

surefire00
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

This is what I use:

Code: Select all

Procedure.s x_strex(var.l,format.s)                                  ; convert int to string using a format field, format characters space and -+#.#
  Protected s.s, fl.l, sl.l, *f.CHAR, *s.CHAR, p.l, sb.l
  ;
  ; *** format integer to string
  ;
  ; in:  var.l    - int var
  ;      f.s      - format
  ; out: .s       - string
  ;
  ; convert int var to string using a pattern
  ;
  ; pattern elements:
  ;
  ; '#' - number or leading zero
  ; ' ' - space, number or (when there's no '+' or '-' used in the format) sign
  ; '+' - positive or negative indicator
  ; '.' - decimal sign
  ;
  ; examples:
  ;
  ; x_strex( 1234,     "###") =     "***"
  ; x_strex( 1234,   "##.##") =   "12.34"
  ; x_strex(-1234,   "##.##") =   "*****"
  ; x_strex(    1,  ".#####") =  ".00001"
  ; x_strex(   -1, "+   .##") = "-   .01"
  ;
  fl = Len(format)
  s = Str(var)
  sl = Len(s)
  ;
  *f.CHAR = @format+fl*#Charsize              ; using two pointers and two counters
  *s.CHAR = @s+sl*#Charsize                   ; *f and *s are the pointers, fl and sl are the counters
  ;
  If PeekC(@format) = '+'                     ; remember if there is a sign in a fixed place
    p = 2
  ElseIf PeekC(@format) = '-'
    p = 1
  Else
    p = 0
  EndIf
  ;
  While fl > 0
    fl = fl-1
    *f = *f-#Charsize                         ; we could be in unicode mode...
    If *f\c = '.'                             ; skip a dot if we pass one
    Else
      sl = sl-1
      If sl >= 0
        *s = *s-#Charsize
        sb = *s\c                             ; sb contains the digit, AND is used as a flag
      EndIf
      Select *f\c
      Case '-'                                ; ah, a sign in a fixed place
        If sb = '-'
          sb = -1                             ; if sb = -1 we've managed to store the sign
        Else
          *f\c = ' '
        EndIf
      Case '+'                                ; same thing for the +
        If sb = '-'
          *f\c = sb
          sb = -1
        EndIf
      Case '#'                                ; if we have data that is not a sign we're gonna fill it in
        If sb = '-' Or sl < 0
          *f\c = '0'                          ; otherwise it's going to be a zero
        Else
          *f\c = *s\c
        EndIf
      Case ' '                                ; if there is no fixed spot for a sign we'll store the minus
        If sb = '-'                           ; immediately on the first space we encounter
          If p = 0
            *f\c = '-'
            sb = -1
          EndIf
        ElseIf sb <> -1 And sl >= 0           ; otherwise we'll just gonna fil it in
          *f\c = *s\c
        EndIf
      EndSelect
    EndIf
  Wend
  ;
  If sb = '-' Or sl > 0                       ; if the sign wasn't stored or there was some data left
    format = LSet("",Len(format),"*")         ; we'll put in some stars to indicate overflow
  EndIf
  ;
  ProcedureReturn format
EndProcedure
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Number Formating function

Post by PB »

I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
surefire00
New User
New User
Posts: 4
Joined: Thu May 11, 2006 2:26 pm
Location: UK

format$

Post by surefire00 »

Thanks for your reply I shall use your code in my project, but I really wanted to know why PureBasic was without such a basic core function. Some others are

d=countdrives()
a$=getdrivelabelname(drive$)

text to speech eg. say("text")

thanks again
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: format$

Post by Kaeru Gaman »

surefire00 wrote:Thanks for your reply I shall use your code in my project, but I really wanted to know why PureBasic was without such a basic core function. Some others are

d=countdrives()
a$=getdrivelabelname(drive$)

text to speech eg. say("text")

thanks again
:shock: :?:
Image

wtf...

what do you think basic BASIC funktions are ...?

*puzzle*


okay, PRINT USING can be counted as a relatively basic function,
but it's not that formatting like that comes completely unsupported:

Code: Select all

value.d = 12345.6789
out$ = RSet( StrD( value, 2 ), 16, "*" )
Debug out$
"countdrives" and "drivelabelname" are extremely Windows specivic,
some relics from the Disk-Operating-System.
'real' Operating Systems don't use "drives" in this specific way.

... and "text to speech".... be serious.
in what way is this a function that deserves to be called elementary by any means...
oh... and have a nice day.
Post Reply