Metric Conversion-VBForums
Results 1 to 7 of 7

Thread: Metric Conversion

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2008
    Posts
    5

    Metric Conversion

    Hello,

    I'm new to the forums, so hello. I have a quick question in regards to a metric conversion program I am working on. The code as is follows:

    Code:
       Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click
            Dim miles As Integer
            Dim yards As Integer
            Dim feet As Integer
            Dim inches As Integer
            Dim meters As Integer
            Dim kilometers As Integer
            Dim centimeters As Double
            Dim totalInches As Integer
    
            miles = CInt(txtMiles.Text)
            yards = CInt(txtYards.Text)
            feet = CInt(txtFeet.Text)
            inches = CInt(txtInches.Text)
    
    
            totalInches = 63360 * miles + 36 * yards + 12 * feet + inches
            meters = (totalInches / 39.37)
            kilometers = Int(meters / 1000)
            centimeters = (meters / 100)
    
            With lstBoxResults.Items
                .Clear()
                .Add("The metric length is:")
                .Add(kilometers & " kilometers")
                .Add(meters & " meters")
                .Add(centimeters & " centimeters")
            End With
    Say you enter the following:
    Miles: 5
    Yards: 20
    Feet: 2
    Inches: 4

    The output is as follows:
    The metric length is:
    8 kilometers (this is correct)
    8066 meters (should be 65 meters)
    80.66 meters (should be 73.50 meters)

    Any help would be much appreciated. Thanks!

  2. #2

    Thread Starter
    New Member
    Join Date
    Mar 2008
    Posts
    5

    Re: Metric Conversion

    Any ideas?

  3. #3
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,441

    Re: Metric Conversion

    Do a Google search for how to use "Mod". It's a math operation that's handy for such things. It gives you "only the remainder" after division:

    15 mod 5 = 0
    15 mod 6 = 3
    15 mod 7 = 1
    15 mod 8 = 7
    15 mod 9 = 6

    15 divided by 5 is exactly 3 times with 0 remaining.
    15 divided by 6 is 2 times with 3 remaining (6x2 = 12. 15 - 12 = 3)
    15 divided by 7 is 2 times with 1 remaining...

    So...
    8066 meters:
    8066 mod 1000 (because 1000m in 1km) = 66

  4. #4
    Frenzied Member
    Join Date
    May 2006
    Location
    Toronto, ON
    Posts
    1,093

    Re: Metric Conversion

    Alternatively to using mod, you can also use the Math.Floor function to pull out the remainders and then use that to calculate what's left over for the meters and centimeters. Personally, I find that easier to read and follow.

    Change kilometers and meters to doubles and use this code:

    vb Code:
    1. kilometers = (totalInches / 39.37) / 1000
    2.         meters = (kilometers - Math.Floor(kilometers)) * 1000
    3.         centimeters = (meters - Math.Floor(meters)) * 100
    4.  
    5.         With lstBoxResults.Items
    6.             .Clear()
    7.             .Add("The metric length is:")
    8.             .Add(Math.Floor(kilometers) & " kilometers")
    9.             .Add(Math.Floor(meters) & " meters")
    10.             .Add(Math.Round(centimeters, 2) & " centimeters")
    11.         End With

  5. #5
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    23,832

    Re: Metric Conversion

    And then there is Math.DivRem to do both the integer division and the mod in a single step. However, after our last flurry of speed testing on this function, it gains you nothing in performance over just doing the division then the mod.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2008
    Posts
    5

    Re: Metric Conversion

    Wow, I can't believe I never thought of mod. Thanks you guys! I tried what you suggested, Tom Sawyer. Works like a charm. I'll definitely be here again, maybe even to provide help if I can .

  7. #7
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    23,832

    Re: Metric Conversion

    Good to have you.
    My usual boring signature: Nothing

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.