Decimal vs Comma will this work
- VB6_to_PBx
- Enthusiast
- Posts: 627
- Joined: Mon May 09, 2011 9:36 am
Re: Decimal vs Comma will this work
•
Last edited by VB6_to_PBx on Thu Jan 15, 2015 10:37 pm, edited 1 time in total.
PureBasic .... making tiny electrons do what you want !
"With every mistake we must surely be learning" - George Harrison
Re: Decimal vs Comma will this work
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.
"Have you tried turning it off and on again ?"
Re: Decimal vs Comma will this work
Hello VB6_to_PBx,
In our everyday life we write commas to separate the decimal part and might use dots to sepatate thousand or million, for the sake of better viewing. But when it comes to electronical maths, like calculators, we are used to dots also. If they show commas it's likely just a 'cosmetic' display.
Now, you're right that in texts or especially via direct input you might receive comma-separated values. I that case you need to take care to only check if the input is valid and if not, than correct it to your needs.
If you also want to care about the output you could change the dot back to comma on export.
Maybe give your application an option to choose between two 'displays' allowing the stringgadget to show commas while underneath you doing the math with dots.
Like what Little John showed you again, you can see in my output above too: Debuging ValD(a$) returns a wrong result, which also puzzled me these days. An operation that returns two different results only because one of them is passed on via having been placed it a stringgadget in between, is highly alerting and must not be possible in a serious calculation.
hope I didn't say anything wrong ~ greets Vera
addendum: I couldn't regard the two prior postings, as they weren't there when I was writing myself.
In our everyday life we write commas to separate the decimal part and might use dots to sepatate thousand or million, for the sake of better viewing. But when it comes to electronical maths, like calculators, we are used to dots also. If they show commas it's likely just a 'cosmetic' display.
Now, you're right that in texts or especially via direct input you might receive comma-separated values. I that case you need to take care to only check if the input is valid and if not, than correct it to your needs.
If you also want to care about the output you could change the dot back to comma on export.
Maybe give your application an option to choose between two 'displays' allowing the stringgadget to show commas while underneath you doing the math with dots.
Like what Little John showed you again, you can see in my output above too: Debuging ValD(a$) returns a wrong result, which also puzzled me these days. An operation that returns two different results only because one of them is passed on via having been placed it a stringgadget in between, is highly alerting and must not be possible in a serious calculation.
hope I didn't say anything wrong ~ greets Vera
addendum: I couldn't regard the two prior postings, as they weren't there when I was writing myself.
Last edited by Vera on Tue Jan 13, 2015 12:40 am, edited 1 time in total.
Two growing code-collections: WinApi-Lib by RSBasic ~ LinuxAPI-Lib by Omi
Missing a download-file on the forums? ~ check out this backup page.
Missing a download-file on the forums? ~ check out this backup page.
Re: Decimal vs Comma will this work
Small code (Window Only) to enter decimal values. Try to enter this values : 1254.45 -1254.45
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()
➽ Windows 11 64-bit - PB 6.21 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti
Sorry for my bad english and the Dunning–Kruger effect
- VB6_to_PBx
- Enthusiast
- Posts: 627
- Joined: Mon May 09, 2011 9:36 am
Re: Decimal vs Comma will this work
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.
luis ,
excellent small amount of Code to get Decimal Separator !!!
i'm going to incorporate this in my Programs , thanks again !
Vera
thanks for feedback and help you've given me !
falsam
thanks very much for your Code !
"CreateRegularExpression(#Numeric, "^\-{0,1}\d*$|^\-{0,1}\d+\.\d{0,"+Str(Decimal)+"}$|^$")"
gives me plenty ideas for future uses .
i'm pretty sure now i can solve Comma vs Decimal problem in my programs with all your examples
Also ,
falsam .... thanks for this animated .GIF , i know it took a lot of time on your part to create it .

PureBasic .... making tiny electrons do what you want !
"With every mistake we must surely be learning" - George Harrison
Re: Decimal vs Comma will this work
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 .


➽ Windows 11 64-bit - PB 6.21 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti
Sorry for my bad english and the Dunning–Kruger effect