Page 1 of 1

Number Formating function

Posted: Wed Dec 17, 2008 5:48 am
by surefire00
I might have missed something, but I can not fund a number formating function such as

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

surefire00

Posted: Wed Dec 17, 2008 8:39 am
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

Re: Number Formating function

Posted: Wed Dec 17, 2008 9:40 am
by PB

format$

Posted: Wed Dec 17, 2008 2:21 pm
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

Re: format$

Posted: Wed Dec 17, 2008 3:05 pm
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...