1. ## 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()
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!

Any ideas?

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

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`

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.

6. ## 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. ## Re: Metric Conversion

Good to have 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
•

Featured