Decimal Separators in VB - ΩJr. Software Articles and Products

This information lives on a web page hosted at the following web address: 'https://omegajunior.globat.com/code/'.

A.E. Veltstra
13 Feb. 2004

Use this custom function to detect the client's system language, to prevent errors caused by translated decimal separators.

Problem
Visual Basic renders a number that represents a date, like 38000.25. IsNumeric(38000.25) returns True, but Year(38000.25) returns type error (13): number appears to be a string.

Cause
The executing computer's Locale doesn't recognise the . as a decimal separator. The Locale depends on user preferences.

Solution
Replace the . with the PointToDecimal() function:


Code
Function PointToDecimal(strNumber)

Dim strDecimalSeparator

'retrieving the actual locale-bound separator
strDecimalSeparator = Mid(CStr(1.1),2,1)

If IsNumeric(strNumber) Then
PointToDecimal = _
CDbl(Replace(strNumber, ".", strDecimalSeparator))
Else
PointToDecimal = CDbl(strNumber)
End If

End Function



Keywords
vb, vba, vbscript, visual basic, locale, decimal, separator, number, comma, point, dot, date, year

Need problem solving?

Talk to me. Let's meet for coffee or over lunch. Mail me at “omegajunior at protonmail dot com”.