CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 1999
    Location
    Islamabad, Pakistan
    Posts
    12

    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?


  2. #2
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    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


    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

  3. #3
    Join Date
    Dec 1999
    Location
    Islamabad, Pakistan
    Posts
    12

    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.


  4. #4
    Join Date
    Dec 1999
    Posts
    128

    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.

  5. #5
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    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





    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

  6. #6
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    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

    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured