|
-
February 28th, 2000, 04:49 AM
#1
Confusion with Val function
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?
-
February 28th, 2000, 07:54 AM
#2
Re: Confusion with Val function
Use the Format$() function and explicitly set the decimal specifier to a period.
Val(Format$(Val("1.23ab"),"#.##"))
should do the trick
-
February 29th, 2000, 12:33 AM
#3
Re: Confusion with Val function
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.
-
February 29th, 2000, 03:26 AM
#4
Re: Confusion with Val function
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 ).
-------------------------
Nick A.
-
February 29th, 2000, 03:58 AM
#5
Re: Confusion with Val function
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
-
March 28th, 2000, 06:11 AM
#6
Re: Confusion with Val function
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
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
|