Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Windows decimal symbol. How to know. How to set it ?

Status
Not open for further replies.

JesperMP

Programmer
Nov 7, 2007
5
DK
Hi.

I have succeeded in writing some scripts that reads and writes values to text files.
What occasionally screws things up is when the decimal symbol is changed in the regional settings of Windows Control panel. You know, to designate the decimal point, in some languages is used the period ("."), and in some other languages is used the comma (",").

To the questions:

If decimal point is changed from "." to "," in Windows, how does a script then interpret a value that is stored as "12.34" ?
The value is read into a string and I write f.ex.
IF IsNumeric(strValue) THEN iValue = strValue

Can I within VBS somehow read the currently set decimal symbol ?

Can I somehow specify within VBS that one or the the other symbol shall be used, thereby overruling the decimal symbol set in Windows ?

Thanks.
 
Maybe;
msgbox "Decimal character is " & Mid(FormatNumber(0.1,1,true,false,-2), 2, 1)
 
>If decimal point is changed from "." to "," in Windows, how does a script then interpret a value that is stored as "12.34"

To process any file on any machine you really need to know the dpchar being used by the reading computer AND the dpchar used by the computer which wrote the file. Consider adding a 'field' in the text file which indicates that.
 
Hi all.

@ Remou.
The SetLocale function seems to that I could force a Language that uses period for the decimal point (US ?).
Dont know if there will be some unwanted sideeffects from doing so. Will investigate it.

@ Hugh.
dpChar sounds interesting.
But is it available in VBS ? I cant find any mention of it in the VBS reference.
Upon searching for something about dpChar I found that some had quoted you on another forum regarding VB (its a small world):
Code:
Some code from Hugh Lerwill: 
Re internationalization; the following may or may not be of interest; 

    Function dpChar$()
        dpChar$ = Mid$(Format$(0.1, "fixed"), 2, 1)
    End Function
    Function Val#(ByVal txt$)
        'Wraps VBA.Val
        ' makes VAL able to handle decimal point chars other than "."
        ' VBA val only works on decimals when the decimal char is "."
        Static normal As Boolean, init As Boolean, dp$
        If Not init Then
            dp$ = dpChar()
            normal = (dp$ = ".")
            init = True
        End If
        If normal Then
            Val = VBA.Val(txt$)
        Else
            Val = VBA.Val(Replace(txt$, dp$, "."))
        End If
        'VB help recommends use of CDbl but..
        'the following does'nt work with strings that contain numbers and text
        ' like eg. "5.4 mp" because the error is tripped and 0 is returned
        'On Error Resume Next
        'Val = CDbl(txt$)
    End Function

I also consider a brute force method of setting the Decimal Point by writing to the registry. But again maybe this will cause strange sideeffects.
 
dpChar is the name of my function to determine the Decimal Point Character on the host computer;

Function dpChar$()
dpChar$ = Mid$(Format$(0.1, "fixed"), 2, 1)
End Function

is the vb6 version;

Function dpChar()
dpChar = Mid(FormatNumber(0.1,1,true,false,-2), 2, 1)
End Function

should I expect do it in vbs...
 
Could this work ?:

Set Shell = CreateObject("Wscript.shell")
Shell.RegWrite "HKCU\Control Panel\International\sDecimal", ".", "REG_SZ"
Shell.RegWrite "HKCU\Control Panel\International\sList", ";", "REG_SZ"

The above should set the decimal point symbol to ".", and the list separator symbol to ";".
And then the user just have to accept this my dictatorial decision.
 
Ah, we crossposted it seems.

I guess that I could use your function to determine the decimal point, and then like in your example substitute the "wrong" for the "correct" one before proceeding with using the data in the next functions.

I have something to work with now.
Thanks :)
 
I'm not sure if it will work however it's generally regarded as bad practice to do that kind of thing to a user.

Unless you are administrating you should adapt your code to accomodate the users current settings.
 
>substitute the "wrong" for the "correct" one before proceeding

yep.

Excel and csv files can be fun too; please come back if you need more.
 
.. it's generally regarded as bad practice to do that kind of thing to a user.
One could consider me as the administrator for my customers.
But I agree. I will try to pursue adapting my application to the users local settings.
 
>One could consider me as the administrator

In that case you could ensure that all users are using (and cannot change)the preferred regional settings for your organisation and simplify your code accordingly.
If your organisation operates over several countries however you are bound to accomodate them all.
 
one way is to convert "," into "." on the machines that have "," as the decimal; and then after your operation is finished, conevert those machines back to "," - just as they were before you tweaked them.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top