|
-
Mar 4th, 2006, 10:35 PM
#1
Thread Starter
Member
[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:
Dim miles, yards, feet, inches, kilometers As Single
Dim totalinches, totalmeters, totalcm As Double
miles = txtmiles.Text * 63360
yards = txtyards.Text * 36
feet = txtfeet.Text * 12
inches = txtinches.Text
totalinches = miles + yards + feet + inches
totalmeters = (FormatNumber(totalinches / 39.37, 0))
totalcm = (FormatNumber(totalinches / 3937, 1))
kilometers = Int(totalmeters / 1000)
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
-
Mar 4th, 2006, 10:42 PM
#2
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
-
Mar 4th, 2006, 10:59 PM
#3
Thread Starter
Member
Re: Length Converter
It gave me : The metric length is 806600 kilometers 66 meters 0 centimeters.
VB Code:
Dim totalinches, totalmeters As Integer
Dim miles As Integer
Dim yards As Integer
Dim feet As Integer
Dim inches As Integer
Dim kilometers, meters, cm As Integer
totalinches = CSng((txtmiles.Text * 63360) + (36 * txtyards.Text) + (12 * txtfeet.Text) + txtinches.Text)
totalmeters = CSng(totalinches / 39.37)
kilometers = totalmeters * 100
totalmeters = totalmeters Mod 100
meters = totalmeters * 1
totalmeters = meters Mod 1
cm = totalmeters \ 100
totalmeters = cm Mod 100
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.
-
Mar 4th, 2006, 11:18 PM
#4
Re: Length Converter
kilometers = totalmeters * 100
Isnt kilometers totalmeters divided by 1000?
-
Mar 5th, 2006, 12:55 AM
#5
Thread Starter
Member
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:
Dim totalinches, totalmeters As Integer
Dim miles As Integer
Dim yards As Integer
Dim feet As Integer
Dim inches As Integer
Dim kilometers, meters, cm As Integer
totalinches = CSng((txtmiles.Text * 63360) + (36 * txtyards.Text) + (12 * txtfeet.Text) + txtinches.Text)
totalmeters = CSng(totalinches / 39.37)
kilometers = totalmeters / 1000
totalmeters = totalmeters Mod 1000
meters = totalmeters / 10000
totalmeters = meters Mod 10000
cm = totalmeters / 100000
totalmeters = cm Mod 100000
txtansmetric.Text = (" The metric length is " & kilometers & " kilometers " & meters & " meters " & cm & " centimeters.")
-
Mar 5th, 2006, 11:12 AM
#6
Thread Starter
Member
Re: Length Converter
Now I tried :
VB Code:
Private Sub btnconvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnconvert.Click
Dim totalinches, totalmeters, totalkilo, totalmeters1, totalcm As Double
totalinches = 63360 * txtmiles.Text + 36 * txtyards.Text + 12 * txtfeet.Text + txtinches.Text 'want to convert everything to centimeters
totalmeters = totalinches / 39.37 'there is 39.37 inches in one meter
totalkilo = Int(totalmeters) / 1000 'there is 1000 meters in a kilometer
totalmeters1 = totalmeters - (Int(totalkilo) * 1000) 'want to find the remainder (8065.735 - (8 X 1000) = 65
totalcm = totalmeters / 100 - totalmeters1 / 100 'want to find remender of remender (80.6 - )
With lstmetric.Items
.Add("The metric length is")
.Add(FormatNumber(totalkilo, 0) & " kilometers")
.Add(FormatNumber(totalmeters, 0) & " meters")
.Add(FormatNumber(totalcm, 1) & " centimeters")
End With
End Sub
Anyone see a problem. It gives me a wrong answer
-
Mar 5th, 2006, 03:16 PM
#7
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.
-
Mar 5th, 2006, 04:53 PM
#8
Thread Starter
Member
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
|