Click to See Complete Forum and Search --> : Confusion with Val function
Atique Ahmad
February 28th, 2000, 03:49 AM
The value returned by Val depends on the regional settings of Windows 95. For example, Val("1.23aB") returns 1,23 if the regional settings are such that the decimal character is comma, not the period. Now, in this case, Val(Val("1.23aB") will return 1 only. How can one take care of this situation. Using CDbl or the like won't help, since these work only in cases like CDbl("1.23") and not CDbl("1.23aB"), etc.
Can the regional settings be bypassed or changed from the Visual Basic program. If yes, then how?
Clearcode
February 28th, 2000, 06:54 AM
Use the Format$() function and explicitly set the decimal specifier to a period.
Val(Format$(Val("1.23ab"),"#.##"))
should do the trick
Atique Ahmad
February 28th, 2000, 11:33 PM
Sorry, it's not working.
Set the regional settings to, say, Swedish (decimal separator is the comma), and then try it. The result is 1 only, not the decimal part.
Nick A.
February 29th, 2000, 02:26 AM
You can see the following in VB6's help on function Val:
Note The Val function recognizes only the period (.) as a valid decimal separator. When different decimal separators are used, as in international applications, use CDbl instead to convert a string to a number.
Try CDbl. If you have any problems, give us a call (or rather, give us a post :) ).
Clearcode
February 29th, 2000, 02:58 AM
You're right - I copied the code out too hastily. The error you encountered is caused by the fact that "Val" is not internationally aware...fortunately the same oversight means that "Str$" isn't either so:
? val(str(val("1.23ab")))
1,23
? val(format$(val("1.23ab")))
1
Clearcode
March 28th, 2000, 05:11 AM
I actually had a similar problem myself, and an enhanced answer is:
'\\ --[InternationalVal]--------------------------------------------------
'\\ The Val function is not internationally aware, but provides more
'\\ functionality than the internationally aware "CDbl" so this
'\\ is a replacement for both
'\\ ----------------------------------------------------------------------
public Function InternationalVal(byval sIn as string) as Double
If IsNumeric(Trim$(sIn)) then
InternationalVal = CDbl(Trim$(sIn))
else
InternationalVal = Val(Trim$(sIn))
End If
End Function
HTH,
Duncan Jones
Clearcode Developments Ltd
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.