CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2006
    Location
    Venezuela
    Posts
    37

    Question Decimal numbers in textboxes, VB6.0 Spanish

    Hi...
    As an Intro:
    I got a program that reports some values in textboxes. Those are checked, if one of them is out of its limits the program will automatically rectify it.

    Then this problem came out. If any value has a comma (because it is a decimal number, and VB put it by default instead a dot) the program, at the time of reading it, will only take the natural part (before the comma). And, of course, want the whole value instead the "before the comma" part.

    Why this is happening? and, Who can I solve it?
    Last edited by elvagonumero1; August 7th, 2006 at 08:55 AM. Reason: Didn't say that is a VB spanish version
    THANKS FOR READING: elvagonumero1
    "el hijo perdío de Venezuela"--> Everyone is UnDefeatable In Dreams, So... Make'em Real
    ...have a good day

  2. #2
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Decimal numbers in textboxes, VB6.0

    I think the problem lies in reading the data, because comma is an item separator for VB.
    Please show the code how you "read" a value.

  3. #3
    Join Date
    Jun 2006
    Location
    Venezuela
    Posts
    37

    Re: Decimal numbers in textboxes, VB6.0

    the address that you gave doesn't work... shows a msg saying that the info displayed there is no longer valid.

    Where else can I go?
    THANKS FOR READING: elvagonumero1
    "el hijo perdío de Venezuela"--> Everyone is UnDefeatable In Dreams, So... Make'em Real
    ...have a good day

  4. #4
    Join Date
    Jul 2006
    Location
    North-West England
    Posts
    42

    Re: Decimal numbers in textboxes, VB6.0

    Sorry, it seems to be only accessible through the intranet that I'm on.

    The gist was that some VB functions are 'locale aware', so will act differently if the settings are American or Spanish and some are not.

    Locale aware methods: CStr, CDbl, CSng, Cint, CLng, CDate, CCur, Format(number)

    Non-locale aware methods: Str, Str$, Val(string)

    So if you use any of the first methods, it should respond appropriately depending on the locale settings, whereas if you were to use Val, it will ignore the comma regardless of locale settings. However, as my locale settings are set to English, I don't know if it does, in fact, work.
    Last edited by Always confused; August 7th, 2006 at 09:27 AM. Reason: typo

  5. #5
    Join Date
    Jun 2006
    Location
    Venezuela
    Posts
    37

    Re: Decimal numbers in textboxes, VB6.0

    This is no my program, but has the same effect.
    It has three textbox and two buttons:
    The fist one is to input some number (eg 5.6).
    The second one show the value of the first one (eg 5,6) (after you click the first button, Command1).
    The third one shows the problem (eg 5) (after you click the second button, Command2).

    Code:
    Private Sub Command1_Click()
    Text2.Text = Val(text1.Text)
    End Sub
    
    Private Sub Command2_Click()
    Text3.Text = Val(Text2.Text)
    End Sub
    THANKS FOR READING: elvagonumero1
    "el hijo perdío de Venezuela"--> Everyone is UnDefeatable In Dreams, So... Make'em Real
    ...have a good day

  6. #6
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Decimal numbers in textboxes, VB6.0

    Yes. The val() function will stop at the comma.

    A workaround could be text3.text=val(replace(Text2.text, ",", ".")

    If there is a comma it will be replaced by "." and val will return a proper result.

  7. #7
    Join Date
    Jun 2006
    Location
    Venezuela
    Posts
    37

    Thumbs up Re: Decimal numbers in textboxes, VB6.0

    It didn't work out...

    but I find a solution, maybe is the more obvious to look in the fist place.

    For someone who got the same problem... go to "control panel", and set the country "local configuration" to English (US).

    It will turn the decimal char, comma "," , into a dot "."

    I hope that my idea is clear enough... and this would be helpful in the future to someone.

    Now i just have to learn how can i make this change from the program while it runs.
    THANKS FOR READING: elvagonumero1
    "el hijo perdío de Venezuela"--> Everyone is UnDefeatable In Dreams, So... Make'em Real
    ...have a good day

  8. #8
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    516

    Re: Decimal numbers in textboxes, VB6.0

    Quote Originally Posted by elvagonumero1
    It didn't work out...

    but I find a solution, maybe is the more obvious to look in the fist place.

    For someone who got the same problem... go to "control panel", and set the country "local configuration" to English (US).

    It will turn the decimal char, comma "," , into a dot "."

    I hope that my idea is clear enough... and this would be helpful in the future to someone.

    Now i just have to learn how can i make this change from the program while it runs.
    I really don't think you want to do this. Changing the region on a machine will most likely have unintended consequences for other running software. I would either only accept numeric input in the TextBoxes and use the CDbl function as Always confused suggested, or replace the Val function with on that is region safe. I was a little bored, so I cooked up something to start with. It also has another tweak that Val doesn't implement--it will return a Variant of the smallest subtype that will fit the return value instead of just returning a Double subtype.
    Code:
    Private Function RegionFreeVal(ByVal vInput As Variant) As Variant
    
        Dim lPos As Long, yBytes() As Byte, yDecSep As Byte, bFloat As Boolean, bNeg As Boolean
        
        On Error GoTo ErrHand
        
        Select Case TypeName(vInput)
            Case "Integer", "Long", "Single", "Double", "Currency", "Byte"      'No conversion needed.
                RegionFreeVal = vInput                                          'Return the input.
                Exit Function
            Case "String"                                                       'Need to convert.
                yDecSep = Asc(Format$(0, "."))                                  'Get the region's decimal separator.
                yBytes = StrConv(CStr(vInput), vbFromUnicode)                   'Load the byte array.
                For lPos = LBound(yBytes) To UBound(yBytes)                     'Loop through it.
                    Select Case yBytes(lPos)
                        Case 9, 10, 13, 32                                      'Tab, CR, LF, Space get ignored.
                        Case 48 To 57                                           'Numeric, add to output.
                            RegionFreeVal = RegionFreeVal & Chr$(yBytes(lPos))
                        Case yDecSep
                            If bFloat Then Exit For                             'Second decimal seperator reached.
                            bFloat = True
                            RegionFreeVal = RegionFreeVal & Chr$(yDecSep)
                        Case 45                                                 'Minus sign.
                            If bNeg Then Exit For                               'Only the first one counts.
                            If Len(CStr(RegionFreeVal)) <> 0 Then Exit For      'Only accept before numbers.
                            bNeg = True
                            RegionFreeVal = "-"
                        Case Else                                               'Non-numeric, so stop adding.
                            Exit For
                    End Select
                Next lPos
            Case Else
                On Error Resume Next                                            'Pass a type mismatch up the stack.
                Call Err.Raise(13, "RegionFreeVal", "Type Mismatch")
                Exit Function
        End Select
    
        'Return a subtype for the smallest variable type.
        If Len(CStr(RegionFreeVal)) <> 0 Then
            On Error Resume Next                                                'Inline error handling.
            If bFloat Then
                RegionFreeVal = CSng(RegionFreeVal)                             'Try to put it in a single.
            Else
                RegionFreeVal = CInt(RegionFreeVal)                             'Try to put it in an integer.
                If Err.Number = 6 Then                                          'Check for overflow.
                    Err.Clear                                                   'Clear the error for the next test.
                    RegionFreeVal = CLng(RegionFreeVal)                         'Try to put it in a long.
                Else
                    On Error GoTo ErrHand                                       'Not overflow, let error handler catch.
                End If
            End If
            If Err.Number = 6 Then                                              'Last chance.
                Err.Clear
                RegionFreeVal = CDbl(RegionFreeVal)                             'Try to stuff it into a double.
                On Error GoTo ErrHand                                           'If that didn't work, just catch it.
            End If
        End If
    
    ErrHand:
        If Err.Number <> 0 Then
            Call MsgBox("Error number " & Err.Number & vbCrLf & Err.Description & "in RegionFreeVal.")
        End If
       
    End Function

  9. #9
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Decimal numbers in textboxes, VB6.0

    Applause. That's what you get, when a good programmer gets bored.

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