Numbers and Decimals

Everything else that doesn't fall into one of the other PB categories.
Lima
User
User
Posts: 43
Joined: Tue Jul 14, 2015 2:52 pm

Numbers and Decimals

Post by Lima »

Is there any way to format numbers in PB? (separation of thousands, decimal ...)
Is there any specific way to enter numbers with decimal or will have to be as 'string'
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: Numbers and Decimals

Post by Keya »

for decimals check the helpfile for functions like StrF() and StrD() [as opposed to Str()]
Lima
User
User
Posts: 43
Joined: Tue Jul 14, 2015 2:52 pm

Re: Numbers and Decimals

Post by Lima »

Thank you very much for your help.

But the moment you enter the data you can enter any character.
After data validation the programmer 'filters' to what you want to be introduced, correct?
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Numbers and Decimals

Post by davido »

DE AA EB
User avatar
BasicallyPure
Enthusiast
Enthusiast
Posts: 539
Joined: Thu Mar 24, 2011 12:40 am
Location: Iowa, USA

Re: Numbers and Decimals

Post by BasicallyPure »

Perhaps this will be useful for you.
Lima wrote:Is there any way to format numbers in PB? (separation of thousands, decimal ...)

Code: Select all

Procedure.s Commatize(text.s, interval=3)
   ;inserts ',' into string every 'interval' characters
   Protected n.i, length.i, output.s
   Protected frac.s =  StringField(text, 2, ".")
   
   If frac : text = StringField(text, 1, ".") : EndIf
   
   length = Len(text)
   
   If length > interval
      For n = 1 To length / interval
         output = "," + Right(text,interval) + output
         text = Left(text,Len(text)-interval)
      Next n
      
      text = LTrim(text + output,",")
   EndIf
   
   If frac : text + "." + frac : EndIf
   
   ProcedureReturn text
EndProcedure

Debug Commatize("123")
Debug Commatize("1234")
Debug Commatize("12345678")
Debug Commatize("123.456")
Debug Commatize("1234.56789")
Debug Commatize("12345678",2)

Lima wrote:Is there any specific way to enter numbers with decimal or will have to be as 'string'

Code: Select all

EnableExplicit

Procedure.s CheckNumeric(iStrGadget.i)
   ;by 'BasicallyPure' 7.19.2012
   ;purpose: limit StringGadget to accept only numeric input
   ;with support for floating point characters: . - + E e
   ;works with Windows and Linux

   Protected.i iCharCount, iTextLength, iAccept, iDecimal, iExp, iNeg, iUpdate
   Protected.s sNewText, sChar, sGadgetText
   
   sGadgetText = GetGadgetText(iStrGadget)
   
   For iCharCount = 1 To Len(sGadgetText)
     
      sChar = Mid(sGadgetText, iCharCount, 1)
      iAccept = #False
     
      Select sChar
         Case "0","1","2","3","4","5","6","7","8","9"
            iAccept = #True
            If iExp = 1 : iExp + 1 : EndIf
         Case "-", "+"
            If iCharCount = 1
               iAccept = #True : iNeg = 1
            ElseIf iExp = 1
               iAccept = #True : iExp + 1
            EndIf
         Case "."
            If Not iDecimal And Not iExp
               iDecimal = #True : iAccept = #True
               If iCharCount - iNeg = 1
                  sChar = "0" + sChar : iUpdate = #True
               EndIf
            EndIf
         Case "E", "e"
            If Not iExp And iCharCount > 1 + iNeg
               iExp = 1 : iAccept = #True
            EndIf
      EndSelect
     
      If iAccept : sNewText + sChar
      Else : iUpdate = #True
      EndIf
     
   Next iCharCount
   
   If iUpdate
      SetGadgetText(iStrGadget, sNewText)
      iTextLength = Len(sNewText)
      ;Set cursor to end of string
      CompilerSelect #PB_Compiler_OS 
         CompilerCase #PB_OS_Linux
            gtk_editable_set_position_(GadgetID(iStrGadget), iTextLength)
         CompilerCase #PB_OS_Windows
            SendMessage_(GadgetID(iStrGadget),#EM_SETSEL,iTextLength,iTextLength)
      CompilerEndSelect
   EndIf

EndProcedure

; usage example
If OpenWindow(0, 0, 0, 200, 140, "Calc", #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_ScreenCentered)
   
   StringGadget(1, 10, 10, 178, 20, "")
   StringGadget(2, 10, 40, 178, 20, "")
   StringGadget(3, 10, 70, 180, 20, "0",#PB_String_BorderLess|#PB_String_ReadOnly)
   ButtonGadget(4, 10, 100, 180, 30, "Sum")
   
   Repeat
      Select WaitWindowEvent(1)
         Case #PB_Event_Gadget
            Define iGadget.i = EventGadget()
           
            If EventType() = #PB_EventType_Change
               Select iGadget
                  Case 1 : CheckNumeric(1)
                  Case 2 : CheckNumeric(2)
               EndSelect
            ElseIf iGadget = 4
               Define dSum.d = ValD(GetGadgetText(1)) + ValD(GetGadgetText(2))
               SetGadgetText(3, RTrim(RTrim(StrD(dSum), "0"), "."))
            EndIf
         Case #PB_Event_CloseWindow
            Break
      EndSelect
   ForEver
   
EndIf

End
BasicallyPure
Until you know everything you know nothing, all you have is what you believe.
User avatar
blueb
Addict
Addict
Posts: 1121
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: Numbers and Decimals

Post by blueb »

This might like this....

Code: Select all

;==================================================================
;
; Author:     Paul    
; Date:       November 20th, 2011
;
; Format Number String
;
; Number.s  - Number To format (in string format) 
; Group.l   - group in bunches of 
; DecDig.l  - number of decimal places 
; DecSep.s  - Decimal separator character 
; GrpSep.s  - Group separator Character 
; Neg.l     - Format negative values 
;             0 = (000)
;             1 = -000
;             2 = - 000
;             3 = 000- 
;             4 = 000 -  
;==================================================================
EnableExplicit ; just for testing.. in case EnableExplicit is desired in users program.
ProcedureDLL.s FormatNum(Number.s,Group.l,DecDig.l,DecSep.s,GrpSep.s,Neg.l)
  Protected Buffer.s
  Protected NF.NUMBERFMT
     
  Buffer.s=Space(255)
  NF.NUMBERFMT\NumDigits=DecDig
  NF\LeadingZero=0
  NF\Grouping=Group
  NF\lpDecimalSep=@DecSep
  NF\lpThousandSep=@GrpSep
  NF\NegativeOrder=Neg
  GetNumberFormat_(0,0,Number,NF,@Buffer,Len(Buffer))
  ProcedureReturn Buffer
EndProcedure


Debug FormatNum("1000000",3,0,".",",",1)
Debug FormatNum("-1000000",3,2,".",",",0)
- It was too lonely at the top.

System : PB 6.21(x64) and Win 11 Pro (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Numbers and Decimals

Post by davido »

@Lima,
Also look at this clever macro by Freak.
http://www.purebasic.fr/english/viewtop ... 36#p424536
DE AA EB
Lima
User
User
Posts: 43
Joined: Tue Jul 14, 2015 2:52 pm

Re: Numbers and Decimals

Post by Lima »

I thank everyone.
The set of proposals will use some solution.
Post Reply