|
-
October 11th, 1999, 07:56 PM
#1
metric conversion
I am trying to write a small program that will convert the entered miles, yards, feet, and inches into kilometers, meters, and centimeters. The number for kilometers comes out fine, but the meters are way off. What doesn't make sense is that the kilometers are found by using the data from meters. The code is as follows:
private Sub cmdConvert_Click()
miles = Val(txtMiles.Text)
yards = Val(txtYards.Text)
feet = Val(txtFeet.Text)
inches = Val(txtInches.Text)
totalInches = Val(63360 * miles + 36 * yards + 12 * feet + inches) ' takes all valuse entered into the text boxes and converts them to inches
totalMeters = Int(totalInches / 39.37) ' this does not come out right, but kilometers do, even though kilometers (below) use this value.
kilometers = Int(totalMeters / 1000) ' works fine.
centimeters = Int(kilometers / 0.01) ' comes out a little higher than it should.
picDisplay.print "The metric length is:"
picDisplay.print kilometers; "kilometers,"
picDisplay.print totalMeters; "meters, and"
picDisplay.print centimeters; "centimeters."
End Sub
If you enter 5 in the box for miles, 20 for yards, 2 for feet, and 4 for inches, the values should read 8 kilometers, 65 meters, and 73.5 centimeters. Can anyone help me?
Thanks!
-
October 15th, 1999, 02:49 PM
#2
Re: metric conversion
Possibly, your data types are set to Integer or Long, thereby causing unexpected rounding. I'd recommend you be sure all variable types are Single or Double, and only perform Int()-style conversions at the time of a final output (and then only if you are only interested in whole numbers).
Reid Allen Robbins
Green River, WY 82935
-
October 16th, 1999, 04:05 PM
#3
Re: metric conversion
I ran your code. The reason the numbers appear to be wrong is using the Int(). It trims off anything past the decimal point so the kilometers shows 8 when it is actually 8.065. That is why the meters comes out closer 8065. You calculated your centimeters by dividing kilometers by .01 insterd of dividing totalmeter by .01. I assume this is a typing mistake ;-).
Your inches to kilometers = 8.06572
Your inches to meters = 8065.72
Your inches to centimeters = 806572
Use int() with care. Try rounding off a number when using int() i.e.:
centimeters = Int(meters / 0.01 + .5)
This lets a number like 9.999999999 show as 10 instead of 9
and a number like 1.1 show as 1 instead of 2
Hope this helps.
-
October 17th, 1999, 06:53 AM
#4
Re: metric conversion
I made a mistake when I wrote 1.1 shows as 1 instead of 2. It should have been -1.1 shows as -1 instead of -2. The int() goes to the lowest round number. This has always been a probllem with me as I think of rounding as going toward 0. If you want just the integer portion of a number be it positive or negative use Fix().
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
|