Re: Decimal vs Comma will this work
Posted: Mon Jan 12, 2015 11:22 pm
•
http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
Procedure.s GetSystemDecimalSep()
Protected Bufsize = GetLocaleInfo_(#LOCALE_USER_DEFAULT, #LOCALE_SDECIMAL, #Null, 0)
Protected Buf$ = Space(Bufsize)
If GetLocaleInfo_(#LOCALE_USER_DEFAULT, #LOCALE_SDECIMAL, @Buf$, Bufsize)
ProcedureReturn Buf$
EndIf
ProcedureReturn ""
EndProcedure
Code: Select all
EnableExplicit
Enumeration Windows
#Mainform
EndEnumeration
Enumeration Gadgets
#Montant1
#Montant2
EndEnumeration
Enumeration RegularExpression
#Numeric
EndEnumeration
Global WindowStyle.i=#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_ScreenCentered|#PB_Window_SizeGadget
Global PreviousText.s, CurrentText.s
Procedure.s UseMask(Number.s, NumDigit.l=2, LeadingZero.l=0, Grouping.l=3, lpDecimalSep.s=".", lpThousandSep.s=" ", NegativeOrder.i=0)
Protected Buffer.s, NF.NUMBERFMT
Buffer.s=Space(255)
NF\NumDigits = NumDigit
NF\LeadingZero=LeadingZero
NF\Grouping=Grouping
NF\lpDecimalSep=@lpDecimalSep
NF\lpThousandSep=@lpThousandSep
NF\NegativeOrder=NegativeOrder
GetNumberFormat_(#LOCALE_SYSTEM_DEFAULT, 0, Number, NF, @Buffer, Len(Buffer))
If StringField(Buffer, 1, ".") = ""
ProcedureReturn "0"+Buffer
Else
ProcedureReturn Buffer
EndIf
EndProcedure
Procedure OnEventGadget()
Protected Gadget = EventGadget()
Protected EndPos
Protected Buffer.s
Select EventType()
Case #PB_EventType_Focus
SetGadgetColor(Gadget, #PB_Gadget_BackColor, RGB(255, 248, 220))
PreviousText = GetGadgetText(Gadget)
Buffer = ReplaceString(PreviousText, " ","") : PreviousText = Buffer
Buffer = ReplaceString(PreviousText, "(","-") : PreviousText = Buffer
Buffer = RemoveString(PreviousText, ")") : PreviousText = Buffer
SetGadgetText(Gadget, PreviousText)
Case #PB_EventType_Change
CurrentText = GetGadgetText(Gadget)
If MatchRegularExpression(#Numeric, CurrentText) = 0
SendMessage_(GadgetID(Gadget), #EM_GETSEL, 0, @endpos)
endpos - 1
SetGadgetText(Gadget, PreviousText)
SendMessage_(GadgetID(Gadget), #EM_SETSEL, endpos, endpos)
Else
PreviousText = CurrentText
EndIf
Case #PB_EventType_LostFocus
SetGadgetText(Gadget, UseMask(PreviousText, 2))
SetGadgetColor(Gadget, #PB_Gadget_BackColor, RGB(255, 255, 255))
PreviousText = ""
EndSelect
EndProcedure
Procedure Open_MainForm()
Protected Decimal = 2 ;Decimal number
CreateRegularExpression(#Numeric, "^\-{0,1}\d*$|^\-{0,1}\d+\.\d{0,"+Str(Decimal)+"}$|^$")
SetGadgetFont(#PB_Default, FontID(LoadFont(#PB_Any ,"", 11)))
OpenWindow(#Mainform, 0, 0, 500, 400, "IsNumeric", WindowStyle)
TextGadget(#PB_Any, 20, 20, 80, 20, "Number 1")
StringGadget(#Montant1, 100, 20, 100, 20, "0.00", #ES_RIGHT)
TextGadget(#PB_Any, 20, 50, 80, 20, "Number 2")
StringGadget(#Montant2, 100, 50, 100, 20, "0.00", #ES_RIGHT)
SetActiveGadget(#Montant1)
BindGadgetEvent(#Montant1, @OnEventGadget())
BindGadgetEvent(#Montant2, @OnEventGadget())
Repeat : Until WaitWindowEvent(10) = #PB_Event_CloseWindow
EndProcedure
Open_MainForm()
luis wrote:Or something like that.Code: Select all
Procedure.s GetSystemDecimalSep() Protected Bufsize = GetLocaleInfo_(#LOCALE_USER_DEFAULT, #LOCALE_SDECIMAL, #Null, 0) Protected Buf$ = Space(Bufsize) If GetLocaleInfo_(#LOCALE_USER_DEFAULT, #LOCALE_SDECIMAL, @Buf$, Bufsize) ProcedureReturn Buf$ EndIf ProcedureReturn "" EndProcedure
Read the user settings to know which separator he's actually using. Could be Italian and using "." anyway, you can't just guess.
Then accept in input just digits and the user sep (only one occurrence of it would be better)
Replace the user sep in the input string with "." before making any calculation. Val() works only with "."
Before writing back the results to screen / printer / whatever do the opposite and restore the separator the user expects.
A lot of timeVB6_to_PBx wrote:falsam .... thanks for this animated .GIF , i know it took a lot of time on your part to create it .