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!

Comma's vs. Decimals for Numbers

Status
Not open for further replies.

meca

Programmer
Aug 29, 2001
5
0
0
US
I am in the US where a decimal "." is used to designate a fractional number (i.e. 3/2 = 1.5). Some international users seem to use a comma "," for the same purpose (i.e. 3/2 = 1,5). How can I modify my visual basic program to handle both conventions?
 
Just write a couple of function that replace the '.' with a ',' and vice versa.

I would make sure that the '.' is used in all calculations and data storage and that the ',' appears only in the visual display for the user. Thanks and Good Luck!

zemp
 
What zemp said is true, but you must control what type of locale your app is running, cos Cxxx Type convertion functions are aware of local definitions:
I'm in Portugal (Portuguese locals) work as:

Converts Evaluate as
=========== ================
Val("3,5") => 3
CDbl("3.5") => 35
CDbl("3,5") => 3.5

I hope this help you with international problems.

Carlos Paiva
 
You should not have to make much of any any conversions at all if written right; if all user input is placed into the correct variable types before further processing (date input into Date Variables, etc.), and all hard coded values are in US format.
There are a few tweaks needed though, such as when using the Val() function even on hard coded numbers:

x=3.5#
?Val(x)

will return 3 and not 3.5 if the local calls for a comma as a decimal separator.

[/b][/i][/u][sub]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
This really takes two steps:

1. Determine the regional setting for the the decimal.
2. Use the Replace function when appropriate
Code:
'Public Declarations
Declare Function GetLocaleInfo Lib "kernel32" Alias _
"GetLocaleInfoA" (ByVal Locale As Long, ByVal LCType As Long, _
ByVal lpLCData As String, ByVal cchData As Long) As Long

Declare Function GetUserDefaultLCID% Lib "kernel32" ()
Public Const LOCALE_SDECIMAL = &HE
Public gtLocalDecimal as String

   'Determine and store the decimal early in your code
   'Use where appropriate (gtLocalDecimal is a Public String)
   gtLocalDecimal = fGetLocale(LOCALE_SDECIMAL)

Public Function fGetLocale(plSettingOfInterest) As String ' Retrieve the regional setting 
	Dim Symbol As String
	Dim lRet1 As Long
	Dim lRet2 As Long
	Dim lpLCDataVar As String
	Dim Pos As Integer
	Dim Locale As Long
	Dim x As Integer
	Dim tTemp As String

	Locale = GetUserDefaultLCID() 
	lRet1 = GetLocaleInfo(Locale, plSettingOfInterest, lpLCDataVar, 0)
	Symbol = String(iRet1, vbNullChar)
	lRet2 = GetLocaleInfo(Locale, plSettingOfInterest, Symbol, lRet1)
	If Len(Symbol) > 0 Then
		Symbol = Replace(Symbol, vbNullChar, "")
		fGetLocale = Trim$(Symbol)
	Else
		fGetLocale = "."
	End If 
End Function
 
However, you will need to change to the correct decimal points and date formats for some SQL statements.

For the date formats, you only need to format the date using the format function to the US format.

For Date values use:

Public Const gcSQLDATE = "\#mm\/dd\/yyyy\#"
Or
Public Const gcSQLDATE = "\'mm\/dd\/yyyy\'"

SqlDate = Format$(TheDate,gcSQLDATE)


Decimal points have to be handled a little different.
But it is still easy to do it that:

Str(TheNumber)

This will convert a number to US format as in:

?Str("1,5")
Returns 1.5

So just use this function in your SQL statements for number values.


If you follow standard programming techniques for the rest, you may not even need to identify the local you are in.

[/b][/i][/u][sub]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top