Link to home
Create AccountLog in
Avatar of itortu
itortuFlag for United States of America

asked on

formatting numbers and strings

i am trying to format a string using a Select Case I am having problems using the Format function
since it seems it replaces the value I pass with the fixed value instead that applying a format.

this part:
                    Else
                        strFormat = "0.00 "
                        intLength = lFieldLength - strFormat.Length
                        If intLength > 0 Then
                            strFormat = strFormat.PadRight(lFieldLength, "0")
                        End If
                    End If

then down below:

 'Return the Final Formatted String
If fftFormat = eFixedFormatTypes.fftAlphaNumeric Or fftFormat = eFixedFormatTypes.fftLeadingZeros Then
   Format_FixedString = UCase(Left(Format$(strTemp, strFormat), lFieldLength))
Else
   Format_FixedString = Format(strTemp, strFormat)  < this replaces a value "1432.790" with "00000.00 "
                                                                                       instead that formatting it like:"01432.790"
   Format_FixedString = Right(Format_FixedString, lFieldLength)
End If
Avatar of itortu
itortu
Flag of United States of America image

ASKER

here is the whole function:

Public Function Format_FixedString(ByVal sStringToFormat As String, ByVal lFieldLength As Long, _
                                       ByVal fftFormat As eFixedFormatTypes, _
                                       Optional ByVal bFillRightToLeft As Boolean = False) As String
        Dim strFunctionName As String = ""
        Dim strFormat As String = ""
        Dim strTemp As String = ""
        Dim intLength As Integer

        Try

            'Initialize
            strFunctionName = "[Format_FixedString]"
            strTemp = Trim(sStringToFormat)

            'Set First Format
            Select Case fftFormat
                Case eFixedFormatTypes.fftLeadingZeros
                    strFormat = New String(CChar(CStr(lFieldLength)), gc_intAsciiZero)
                    bFillRightToLeft = True

                Case eFixedFormatTypes.fftNumeric
                    strFormat = "0.00"
                    intLength = lFieldLength - strFormat.Length
                    If intLength > 0 Then strFormat = New String(CChar(CStr(intLength)), gc_intAsciiZero) & strFormat
                    bFillRightToLeft = True

                Case eFixedFormatTypes.fftSignedNumeric

                    If CLng(strTemp) < 0 Then
                        strFormat = "0.00-"
                        intLength = lFieldLength - strFormat.Length
                        If intLength > 0 Then
                            strFormat = New String(CChar(CStr(intLength)), gc_intAsciiZero) & strFormat
                            strTemp = CStr(Math.Abs(CDec(strTemp)))
                        End If
                    Else
                        strFormat = "0.00 "
                        intLength = lFieldLength - strFormat.Length
                        If intLength > 0 Then
                            strFormat = strFormat.PadRight(lFieldLength, "0")
                        End If
                    End If

                    bFillRightToLeft = True

                Case Else
                    strFormat = New String(CChar(CStr(lFieldLength)), gc_intAsciiAtSign)
            End Select

            'Check Fill from Left To Right Flag
            If bFillRightToLeft = False And lFieldLength >= Len(strTemp) Then
                strTemp = strTemp & Space(lFieldLength - Len(strTemp))
            ElseIf Trim(strTemp) = "" Then
                strTemp = Space(lFieldLength)
            End If


        Catch ex As Exception
            Msgbox("Error - " & ex.ToString, Err.Description, strFunctionName)
        End Try

        'Return the Final Formatted String
        If fftFormat = eFixedFormatTypes.fftAlphaNumeric Or fftFormat = eFixedFormatTypes.fftLeadingZeros Then
            Format_FixedString = UCase(Left(Format$(strTemp, strFormat), lFieldLength))
        Else
            Format_FixedString = Format(strTemp, strFormat)
            Format_FixedString = Right(Format_FixedString, lFieldLength)
        End If

    End Function
Avatar of Mike Tomlinson
This is because the Format() function works differently in VB.Net than in VB6...

This VB6 code:

    Dim strValue As String
    Dim strFormat As String
    Dim strResult As String
   
    strValue = "1432.790"
    strFormat = "00000.00"
    strResult = Format(strValue, strFormat)
    Debug.Print "strValue = " & strValue
    Debug.Print "strFormat = " & strFormat
    Debug.Print "strResult = " & strResult

Produces:

    strValue = 1432.790
    strFormat = 00000.00
    strResult = 01432.79

While in VB.Net, you need an actual NUMERIC variable to pass into Format():

        Dim strValue As String
        Dim numValue As Decimal
        Dim strFormat As String
        Dim strResult As String

        strValue = "1432.790"
        strFormat = "00000.00"
        numValue = Decimal.Parse(strValue)
        strResult = Format(numValue, strFormat)
        Debug.Print("strValue = " & strValue)
        Debug.Print("numValue = " & numValue.ToString)
        Debug.Print("strFormat = " & strFormat)
        Debug.Print("strResult = " & strResult)

Which produced:

    strValue = 1432.790
    numValue = 1432.790
    strFormat = 00000.00
    strResult = 01432.79
Though, in VB.Net, you would be more likely to see this:

        Dim numValue As Decimal = 1432.79
        Dim strFormat As String = "00000.00"
        Dim strResult As String = numValue.ToString(strFormat)
        Debug.Print("numValue = " & numValue.ToString)
        Debug.Print("strFormat = " & strFormat)
        Debug.Print("strResult = " & strResult)

Producing:

    numValue = 1432.79
    strFormat = 00000.00
    strResult = 01432.79
SOLUTION
Avatar of cheddar73
cheddar73

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of itortu

ASKER

but what happens when the passed value contains only letters, or a mix of both?
Avatar of itortu

ASKER

i jst tried your code and for the numbers it works nice. but then if the passed value is lets say "AA"

then the letters are replaced with zeros on this part of the function:

Format_FixedString = Format(strTemp, strFormat)

Avatar of cheddar73
cheddar73

If a value contains letters, a numeric format doesn't apply anyway.  What behavior are you expecting from the Format() function in that case?

Format("ABCDEFG", "00000.00")  ' <---- Makes no sense
Are you converting a VB6 function to VB.Net?

You might want to convert your String value to a Numeric value, and then pass the "format" into ToString() as I showed in my second post.

Check to see if your String value is a valid numeric by enclonsing a xxx.Parse() call in a Try...Catch block, where "xxx" is replaced by Integer, Decimal, etc..

For VB.Net 2005, you can use TryParse() instead of Parse().

It seems like the functionality you are trying to achieve is mostly already there in .Net....

There are also NAMED numeric formats you can use:
http://msdn2.microsoft.com/en-us/library/dwhawy9k.aspx
Avatar of itortu

ASKER

i understand, but let me explain to the best of my abilities..


 Public Property CompanyNumber() As String    << let's say company number is actually a code "MJ"
   Get
     CompanyNumber = m_strCompanyNumber
   End Get
   Set(ByVal vData As String)
     m_strCompanyNumber = Format_FixedString(Trim(vData), 2, basMain.eFixedFormatTypes.fftAlphaNumeric)
   End Set
 End Property

then the enums are sorted like this:
Public Enum eFixedFormatTypes As Integer
        fftLeadingZeros = 0
        fftNumeric = 1
        fftSignedNumeric = 2
        fftAlphaNumeric = 3
    End Enum

there is a value for alphanumeric too. and the value for client needs to pass through the Format_FixedString function.
Avatar of itortu

ASKER

or is it possible that instead of going to the function the formatting is done in the property code?
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Avatar of itortu

ASKER

guys I am terribly lost with this function, I know just enough vb6 to get around and now converting vb6 to vb.net is making me feel sick. I uploaded a text file with some data and the formatting function. please if you could help me writing this I would appreciate it greatly besides of doing me a really big favor.

I understand if this is a lot to ask from you, just in case, this is the link to the file:

https://filedb.experts-exchange.com/incoming/ee-stuff/3701-formatting.txt

thank you.
Avatar of itortu

ASKER

i just saw the comment for the alpha numerics, let me try it, i also have dates to deal with

dates tha come in format 2/15/2007 and a format mm/dd/yyyy needs to be applied to it.
Avatar of itortu

ASKER

would you please let me know if I will be able to receive more help? thank you.

sorry for bein ganxious, i have been playing with this code way too long and i am freaking out now.