VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Converts a currency formated string (BR, US, EU, anyone) into a float POINT(single) variable, neede

by Gabriel Tavares de Oliveira Castellani (5 Submissions)
Category: Math/Dates
Compatability: Visual Basic 5.0
Difficulty: Unknown Difficulty
Originally Published: Sat 3rd June 2006
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Converts a currency formated string (BR, US, EU, anyone) into a float POINT(single) variable, needed for calculations in VB. Uses API.

API Declarations



'Configurações do Windows
Global gstrSeparadorDecimal As String
Global gstrSeparadorMilhar As String
Global gstrSeparadorData As String

Rate Converts a currency formated string (BR, US, EU, anyone) into a float POINT(single) variable, neede



'Carrega os padrões de ponto decimal, separador de milhar e
'data do Windows

Sub CarregaconfiguracoesRegionais()

    Dim ret As Long
    Dim auxSep As String * 2

    'le separador decimal do win.ini
    ret = GetProfileString("intl", "sDecimal", ".", auxSep, 2)
    gstrSeparadorDecimal = Left$(auxSep, 1)

    'le separador de milhar do win.ini
    ret = GetProfileString("intl", "sThousand", ",", auxSep, 2)
    gstrSeparadorMilhar = Left$(auxSep, 1)

    'le separador de data do win.ini
    ret = GetProfileString("intl", "sDate", "/", auxSep, 2)
    gstrSeparadorData = Left$(auxSep, 1)

End Sub


'Function ArrumaValor()

'Gabriel Tavares de Oliveira Castellani

'Converte uma string (Valor) numérica decimal, qualquer que
'seja sua formatação (brasileira, americana,...) para single
'com PONTO decimal

Function ArrumaValor(ByVal Valor As String) As Double

    Dim C As String
    Dim d As Byte
    Dim i As Integer, j As Integer
    Dim Palavra As String
    Dim jahAchouDecimal As Boolean
    
    Dim NUMEROS As String
    
    NUMEROS = "0123456789-+" & Trim$(gstrSeparadorDecimal) & Trim$(gstrSeparadorMilhar)

    Palavra = Space$(Len(Valor))
    jahAchouDecimal = False

    For i = Len(Valor) To 1 Step -1
    
        C = Mid$(Valor, i, 1)
        
        If InStr(1, NUMEROS, C) = 0 Then Exit For
        
        If C = gstrSeparadorDecimal Or C = gstrSeparadorMilhar Then
            If Not jahAchouDecimal Then
                jahAchouDecimal = True
                Mid$(Palavra, i, 1) = "."
            End If
        ElseIf C = "-" Or C = "+" Then
            Palavra = C & Palavra
            Exit For
        Else
            Mid$(Palavra, i, 1) = C
        End If
        
    Next

    ArrumaValor = Val(Palavra)

End Function

'************************************************
'Exemplo (example)
'************************************************
Sub Form_Load()
    CarregaConfiguracoesRegionais  'Chamada apenas uma vez no começo do programa
                                   'Called only once in program's 1st sub
    MsgBox "BR: " & ArrumaValor("R$ 1.527,89") & vbCrLf & "US: " & ArrumaValor("US$ 1,527.89")
    'Resultado/Result:    BR: 1527.89  //  US: 1527.89
End Sub

Download this snippet    Add to My Saved Code

Converts a currency formated string (BR, US, EU, anyone) into a float POINT(single) variable, neede Comments

No comments have been posted about Converts a currency formated string (BR, US, EU, anyone) into a float POINT(single) variable, neede. Why not be the first to post a comment about Converts a currency formated string (BR, US, EU, anyone) into a float POINT(single) variable, neede.

Post your comment

Subject:
Message:
0/1000 characters