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