|
-
Sep 17th, 2009, 10:29 PM
#1
[RESOLVED] Better way to get locale's decimal character?
I know I've seen this posted several times on the forum but searching didn't bring up anything relevant.
I want to know how to get the decimal character for the current locale/regional settings.
Example: Most use a standard period as decimal, while other countries use a comma.
Right now, I just did a small "hack" and I'm not sure how reliable it will be:
MsgBox Mid$(CStr(Format(0, "fixed")), 2, 1)
-
Sep 18th, 2009, 02:16 AM
#2
Re: Better way to get locale's decimal character?
You could do this:
Code:
dim a as currency
dim b as string
a = 1.2
b = a
msgbox(mid(a,2,1))
But of course the "best" way would be to read out the registry
-
Sep 18th, 2009, 04:30 AM
#3
Addicted Member
Re: Better way to get locale's decimal character?
i test it first like:
result can be 2,5 or 2.5 depend on regional settings.
i dont know, but is it what you're looking for?
-
Sep 18th, 2009, 05:01 AM
#4
Re: Better way to get locale's decimal character?
i guess the real question is, why you should need to know?
your code should be able to work correctly for either
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Sep 18th, 2009, 05:09 AM
#5
Re: Better way to get locale's decimal character?
The problem is that 1.000 could represent 1 (if . is the decimal separator) or 1000 (of , is the decimal separator and . is the thousand separator)
-
Sep 18th, 2009, 07:42 AM
#6
Re: Better way to get locale's decimal character?
I agree with westconn1. There should never be any need to know what the local separator is. All the functions required to handle numerical strings correctly to locale are available in VB. It is also naive to assume the the separator will be either dot or comma, it can be anything the user wants it to be.
In the scenario where a system of one locale passes numbers to a system of a differing locale, these numbers should not be sent as strings. Or at least if they have to be strings they should be formatted in a particular way.
-
Sep 18th, 2009, 08:43 AM
#7
Re: Better way to get locale's decimal character?
Agree with the last few posters.
To convert string to regional settings number system, use vb conversion functions like CLng, CDbl, CCur, etc. Do not use Val() as it is not regional aware.
-
Sep 18th, 2009, 10:56 AM
#8
Re: Better way to get locale's decimal character?
I was doing a check in the _KeyPress() event for a decimal character, so it only allows them to enter a decimal if there isn't already one there. Easy enough, assuming I know the correct decimal character.
-
Sep 18th, 2009, 11:17 AM
#9
Re: Better way to get locale's decimal character?
IsNumeric() will return false if more than one decimal separator is present (regardless of what that separator is)
The KeyPress event does not enable policing of right click menu pasted data. It's common to do some further validation in say the Change or Validate event, which in turn might be a better place to assess the validity of the number as a whole.
I still don't think you need to know what the separator is.
-
Sep 18th, 2009, 11:18 AM
#10
Re: Better way to get locale's decimal character?
Maybe post 2 or 3 would suit you. Of course if you cache this character on form load, if someone changes their regional settings while your app is loaded, it would cause you problems. Then again, you can always calculate it on the focus event of the textbox.
But Milk's point is a very good one; pasting is something to deal with.
-
Sep 18th, 2009, 03:50 PM
#11
Re: Better way to get locale's decimal character?
2 and 3 are basically what mine does but with more code. 
But I will probably use the IsNumeric() function.
-
Sep 18th, 2009, 06:58 PM
#12
Re: [RESOLVED] Better way to get locale's decimal character?
This may not apply to you Dan(?) but speaking as someone who obsesses a little too much over the efficiency of code, dealing with user keyboard input is not an area to worry about. Really awful inefficient keyboard policing will probably still run much faster than anyone can possibly type.
-
Sep 18th, 2009, 11:25 PM
#13
Re: [RESOLVED] Better way to get locale's decimal character?
Just use one of the variations on Mid$(CStr(1.1), 2, 1) since that is probably as quick and reliable as you'll get.
Another important reason to have this information is for formatting values for transmission between computers. If you serialize data to Strings (or even XML) you can't rely on CStr() and CCur()/Cdbl()/CSng() alone because these will use the local settings. This means a message sent from the U.K. to France would be misinterpreted, since the former uses "." and the latter ",".
While it might seem a bit Anglo-centric, the common medium of exchange is to use "." so you need to always perform this conversion. Thus most serializer/parsers contain something along the lines of:
Code:
Private Function UnivCCur(ByVal Value)
UnivCCur = CCur(Replace(Value, ".", DecimalPt))
End Function
Private Function UnivCDbl(ByVal Value)
UnivCDbl = CDbl(Replace(Value, ".", DecimalPt))
End Function
Private Function UnivCSng(ByVal Value)
UnivCSng = CSng(Replace(Value, ".", DecimalPt))
End Function
Private Function UnivCStr(ByVal Value)
UnivCStr = Replace(CStr(Value), DecimalPt, ".")
End Function
Where DecimalPt is set during the Class_Initialize event.
As written the code above works in a VB, VBA, or VBScript class. For VB/VBA you might want stronger typing than the default Variants, but the logic is the same.
Yet another reason is for formatting CSV data. Not only do you need to worry about "," as decimal point, in such a locale the field delimiter is usually ";" instead of "," as well.
-
Sep 18th, 2009, 11:31 PM
#14
Re: [RESOLVED] Better way to get locale's decimal character?
You can also detect a locale change by using the VB6 SysInfo control. Whenever the SettingChanged event occurs, refresh your DecimalPt (or equivalent). If you're picky you can check the event's section parameter for the value "intl" which means the locale changed.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|