Results 1 to 3 of 3
  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2011
    Virginia Beach, VA

    Formatting using Strings.Format() or Format() ?

    I haven't found the answer in my book yet, so I thought I'd jump ahead and just ask the question here...

    I have the following code in my VB6 program that formats a string representation of a number to a decimal foot number. I am having trouble formatting the same string in VB.NET. When I read the help pages, it appears that nothing has changed and this should work like it did in VB6.

    Function conv(fis As String) As Double
    'use this function to convert feet inches sixteenths
    'to decimal foot. (240308 becomes 24.2917)
        decft = Format(fis, "000000") 'pad string length
        F = Mid(decft, 1, Len(decft) - 4)
        I = Mid(decft, Len(decft) - 3, 2) / 12
        S = Mid(decft, Len(decft) - 1, 2) / 192
        conv = Format(F + I + S, ".0000")
        Exit Function
    End Function
    When I use the exact same code in VB.NET, nothing happens. I don't get any error messages either...

    Any help would be greatly appreciated...

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Sydney, Australia

    Re: Formatting using Strings.Format() or Format() ?

    You have posted in the wrong forum. CodeBank forums are for you to post code samples to share with others, not to ask questions. You post VB.NET questions in the VB.NET forum. I have asked the mods to move this thread.

    As for the question, VB.NET supports a lot of what VB6 supported specifically so that VB6 code could be ported to VB.NET with little to no change. That doesn't mean that doing so is a good idea though. What you've posted there is quite horrible VB.NET code. In VB.NET, you should always use String.Format in preference to Format but, when all you want to format is a single value, you should use its own ToString method instead. In this case though, there is absolutely no reason to be formatting anything. If you want a good VB.NET function to do that job then it would look something like this:
    Private Function ConvertFISToDecimal(fis As String) As Double
        'Validate the entire input.
        If Not Integer.TryParse(fis, Nothing) Then
            Throw New ArgumentException("The input was not in the correct format.", "fis")
        End If
        'Pad the input if inches and/or feet are missing.
        fis = fis.PadLeft(6, "0"c)
        'All but the last four characters represent the full feet.
        Dim feetLength = fis.Length - 4
        'Separate the input.
        Dim feet = CInt(fis.Substring(0, feetLength))
        Dim inches = CInt(fis.Substring(feetLength, 2))
        Dim sixteenths = CInt(fis.Substring(feetLength + 2, 2))
        'Validate the parts.
        If inches > 11 OrElse sixteenths > 15 Then
            Throw New ArgumentException("The input was not in the correct format.", "fis")
        End If
        Return feet + (inches / 12) + (sixteenths / (16 * 12))
    End Function
    If you want to round the result to a specific number of decimal places then you can do that with Math.Round, either inside the method or outside.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Bristol, UK

    Re: Formatting using Strings.Format() or Format() ?

    Thread moved from the 'CodeBank VB.Net' forum (which is for you to post working code examples, not questions) to the 'VB.Net' forum

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

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.