
Mar 3rd, 2008, 10:16 PM
#1
Thread Starter
New Member
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!

Mar 4th, 2008, 07:28 AM
#2
Thread Starter
New Member

Mar 4th, 2008, 08:28 AM
#3
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

Mar 4th, 2008, 08:57 AM
#4
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:
kilometers = (totalInches / 39.37) / 1000 meters = (kilometers  Math.Floor(kilometers)) * 1000 centimeters = (meters  Math.Floor(meters)) * 100 With lstBoxResults.Items .Clear() .Add("The metric length is:") .Add(Math.Floor(kilometers) & " kilometers") .Add(Math.Floor(meters) & " meters") .Add(Math.Round(centimeters, 2) & " centimeters") End With

Mar 4th, 2008, 09:19 AM
#5
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

Mar 4th, 2008, 02:20 PM
#6
Thread Starter
New Member
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 .

Mar 4th, 2008, 02:23 PM
#7
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

Forum Rules

Click Here to Expand Forum to Full Width
Survey posted by VBForums.
