Results 1 to 8 of 8

Thread: [RESOLVED] Length Converter

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    35

    Resolved [RESOLVED] Length Converter



    total inches = 63360*miles + 36*yards + 12*feet +inches
    I need to calculate the total inches
    total meters = total inches/39.97
    Then convert it in meters
    Kilometers = Int(meters/1000)
    And then covert that into Metric. Now im lost. I tried :
    VB Code:
    1. Dim miles, yards, feet, inches, kilometers As Single
    2.         Dim totalinches, totalmeters, totalcm As Double
    3.         miles = txtmiles.Text * 63360
    4.         yards = txtyards.Text * 36
    5.         feet = txtfeet.Text * 12
    6.         inches = txtinches.Text
    7.         totalinches = miles + yards + feet + inches
    8.         totalmeters = (FormatNumber(totalinches / 39.37, 0))
    9.         totalcm = (FormatNumber(totalinches / 3937, 1))
    10.         kilometers = Int(totalmeters / 1000)
    11.         txtansmetric.Text = "The metric length is         " & kilometers & " kilometers                  " & totalmeters & " meters                    " & totalcm & " centimeters."
    which works, but it shows the Complete inches in Kilometers, the Complete inches in meters, and the Complete inches in Centimeters. I need to tamke it calculate perfectly, like:

    Miles = 5
    Yards = 20
    Feet = 2
    Inches = 4

    which would result in:
    the metric length is 8 kilometers, 65 meters, and 73.5 centimeters
    Thanks to anyone that can help

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Length Converter

    Check out this thread.. does a similar methodology, but with change (dollars, quarters, nickels, etc...) Maybe it can give you some direction...

    http://www.vbforums.com/showthread.php?t=390890

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    35

    Re: Length Converter

    It gave me : The metric length is 806600 kilometers 66 meters 0 centimeters.
    VB Code:
    1. Dim totalinches, totalmeters As Integer
    2.         Dim miles As Integer
    3.         Dim yards As Integer
    4.         Dim feet As Integer
    5.         Dim inches As Integer
    6.         Dim kilometers, meters, cm As Integer
    7.  
    8.         totalinches = CSng((txtmiles.Text * 63360) + (36 * txtyards.Text) + (12 * txtfeet.Text) + txtinches.Text)
    9.         totalmeters = CSng(totalinches / 39.37)
    10.  
    11.         kilometers = totalmeters * 100
    12.         totalmeters = totalmeters Mod 100
    13.         meters = totalmeters * 1
    14.         totalmeters = meters Mod 1
    15.         cm = totalmeters \ 100
    16.         totalmeters = cm Mod 100
    17.         txtansmetric.Text = (" The metric length is " & kilometers & " kilometers " & meters & " meters " & cm & " centimeters.")

    Can you see anything wrong? I think its in the kilometers, meters and cm part. It doesnt make sense with the Mod.

  4. #4
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Length Converter

    kilometers = totalmeters * 100

    Isnt kilometers totalmeters divided by 1000?

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    35

    Re: Length Converter

    ya, now the kilometers work, but the rest doesnt. What would I do for meters?
    It gives me :
    The metric length is 8 kilometers 0 meters 0 centimeters.
    When I input 5 miles, 20 yards, 4 feet and 2 inches.

    My code is

    VB Code:
    1. Dim totalinches, totalmeters As Integer
    2.         Dim miles As Integer
    3.         Dim yards As Integer
    4.         Dim feet As Integer
    5.         Dim inches As Integer
    6.         Dim kilometers, meters, cm As Integer
    7.  
    8.         totalinches = CSng((txtmiles.Text * 63360) + (36 * txtyards.Text) + (12 * txtfeet.Text) + txtinches.Text)
    9.         totalmeters = CSng(totalinches / 39.37)
    10.  
    11.         kilometers = totalmeters / 1000
    12.         totalmeters = totalmeters Mod 1000
    13.         meters = totalmeters / 10000
    14.         totalmeters = meters Mod 10000
    15.         cm = totalmeters / 100000
    16.         totalmeters = cm Mod 100000
    17.         txtansmetric.Text = (" The metric length is " & kilometers & " kilometers " & meters & " meters " & cm & " centimeters.")

  6. #6

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    35

    Re: Length Converter

    Now I tried :
    VB Code:
    1. Private Sub btnconvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnconvert.Click
    2.         Dim totalinches, totalmeters, totalkilo, totalmeters1, totalcm As Double
    3.  
    4.         totalinches = 63360 * txtmiles.Text + 36 * txtyards.Text + 12 * txtfeet.Text + txtinches.Text 'want to convert everything to centimeters
    5.         totalmeters = totalinches / 39.37 'there is 39.37 inches in one meter
    6.         totalkilo = Int(totalmeters) / 1000 'there is 1000 meters in a kilometer
    7.         totalmeters1 = totalmeters - (Int(totalkilo) * 1000) 'want to find the remainder (8065.735 - (8 X 1000) = 65
    8.         totalcm = totalmeters / 100 - totalmeters1 / 100 'want to find remender of remender (80.6 - )
    9.  
    10.  
    11.         With lstmetric.Items
    12.             .Add("The metric length is")
    13.             .Add(FormatNumber(totalkilo, 0) & " kilometers")
    14.             .Add(FormatNumber(totalmeters, 0) & " meters")
    15.             .Add(FormatNumber(totalcm, 1) & " centimeters")
    16.         End With
    17.     End Sub

    Anyone see a problem. It gives me a wrong answer

  7. #7
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Length Converter

    If you notice the other post, they are using "\" and not "/" which is very important, they also use Mod....

    From Documentation on the "\" operator...
    Divides two numbers and returns an integer result. The result is the integer quotient of number1 and number2, dropping the remainder.

  8. #8

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    35

    Re: Length Converter

    Thank you

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