converting centimeters to feet and inch

  • Thread starter cozturk via AccessMonster.com
  • Start date
C

cozturk via AccessMonster.com

Sorry, my brain got totally dead on this.

I have a form converting metric to US units and vice versa. for converting
height in format ft'in" (such as 5'3") to cms, I simply used =([ft]*30,48)+(
[in]*2,54) formula. Stuck on doing vice versa. I need to divide the cms by 30.
48 to get the feet value, then extract the remainder and convert to inches.

for example, 163 cms = 5.34 feet. I need to get the 5 feet on one hand and
convert the .34 to inches by multiplying it with 12 which will make 5 feet 4
inches.

I just couldn't & appreciate any assistance

Thanks in advance,
Celal
 
M

Marshall Barton

cozturk said:
Sorry, my brain got totally dead on this.

I have a form converting metric to US units and vice versa. for converting
height in format ft'in" (such as 5'3") to cms, I simply used =([ft]*30,48)+(
[in]*2,54) formula. Stuck on doing vice versa. I need to divide the cms by 30.
48 to get the feet value, then extract the remainder and convert to inches.

for example, 163 cms = 5.34 feet. I need to get the 5 feet on one hand and
convert the .34 to inches by multiplying it with 12 which will make 5 feet 4
inches.


ft = Int(cm / 30.48)
in = Round((cm - ft * 30.48) / 2.54, 0)
 
C

cozturk via AccessMonster.com

Marshall said:
Sorry, my brain got totally dead on this.
[quoted text clipped - 6 lines]
convert the .34 to inches by multiplying it with 12 which will make 5 feet 4
inches.

ft = Int(cm / 30.48)
in = Round((cm - ft * 30.48) / 2.54, 0)

Thanks so much for the quick response Marshal. However, I think I need the
result as text as there will be ' for feet and " for inch.

your formula gives me "00"

for example on 180 cms which is 5.906 feet which is 5'11"
another could be 210 cms which is 6.89 feet which is 6'10"

also, do you think i have to write an after update or lost focus event?? i
just simply wrote
"=[kilos]/0,45" at the control source for "pounds" textbox which gives me an
immediate conversion on kilos to pounds with no event procedures or queries.
 
C

cozturk via AccessMonster.com

cozturk said:
[quoted text clipped - 4 lines]
ft = Int(cm / 30.48)
in = Round((cm - ft * 30.48) / 2.54, 0)

Thanks so much for the quick response Marshal. However, I think I need the
result as text as there will be ' for feet and " for inch.

your formula gives me "00"

for example on 180 cms which is 5.906 feet which is 5'11"
another could be 210 cms which is 6.89 feet which is 6'10"

also, do you think i have to write an after update or lost focus event?? i
just simply wrote
"=[kilos]/0,45" at the control source for "pounds" textbox which gives me an
immediate conversion on kilos to pounds with no event procedures or queries.


sorry, didn't write the code which gave me the "00" result:

Private Sub cms_AfterUpdate()

ft = Int(cm / 30.48)
inch = Round((cm - ft * 30.48) / 2.54, 2)

Me.feet = ft & inch

End Sub
 
M

Marshall Barton

cozturk said:
cozturk said:
Sorry, my brain got totally dead on this.
[quoted text clipped - 4 lines]
ft = Int(cm / 30.48)
in = Round((cm - ft * 30.48) / 2.54, 0)

Thanks so much for the quick response Marshal. However, I think I need the
result as text as there will be ' for feet and " for inch.

your formula gives me "00"

for example on 180 cms which is 5.906 feet which is 5'11"
another could be 210 cms which is 6.89 feet which is 6'10"

also, do you think i have to write an after update or lost focus event?? i
just simply wrote
"=[kilos]/0,45" at the control source for "pounds" textbox which gives me an
immediate conversion on kilos to pounds with no event procedures or queries.


sorry, didn't write the code which gave me the "00" result:

Private Sub cms_AfterUpdate()

ft = Int(cm / 30.48)
inch = Round((cm - ft * 30.48) / 2.54, 2)

Me.feet = ft & inch

End Sub


Well, that code looks like there are a couple of things
going on. First, I suspect that the centimeters text box is
not named cm.

Second, you never declared the ft and inch variables (a poor
practice), which tells me that you do not have the
OPTION EXPLICIT
statement at the top of the module. This is a good practice
because you will be immediately notified when you use an
undeclared variable, especially when you misspell a name.
Based on your code the name should be cms:

Private Sub cms_AfterUpdate()
Dim ft As Integer, inch As Currency

ft = Int(cms / 30.48)
inch = Round((cms - ft * 30.48) / 2.54, 2)

Me.feet = ft "'" & inch & """"

End Sub


If you want to use a text box expression instead of VBA code
in an event procedure, try:

=Int(cm / 30.48) & "'" & Round((cm - Int(cm / 30.48) *
30.48) / 2.54, 2) & """"

But I suspect that you will want further refinements that
nay make the expression so complex that it could be near
incomprehensible.
 
C

cozturk via AccessMonster.com

In fact, I cut off some lines. I do use Option Explicit and dim every
variable but to keep it short, didn't post here.

the text box for centimeters is named cms, so i changed it accordingly.

before i try your new suggestion (cause it's almost 5:00am here and i'm with
no sleep yet), why do we declare inch As Currency??

Still, i believe i'd need to get the intiger part of the result in one
variable and change to string, then get the decimal part of the result and do
a calculation and save the final result as string again.

am i lost in the logic of things???

Marshall said:
[quoted text clipped - 25 lines]

Well, that code looks like there are a couple of things
going on. First, I suspect that the centimeters text box is
not named cm.

Second, you never declared the ft and inch variables (a poor
practice), which tells me that you do not have the
OPTION EXPLICIT
statement at the top of the module. This is a good practice
because you will be immediately notified when you use an
undeclared variable, especially when you misspell a name.
Based on your code the name should be cms:

Private Sub cms_AfterUpdate()
Dim ft As Integer, inch As Currency

ft = Int(cms / 30.48)
inch = Round((cms - ft * 30.48) / 2.54, 2)

Me.feet = ft "'" & inch & """"

End Sub

If you want to use a text box expression instead of VBA code
in an event procedure, try:

=Int(cm / 30.48) & "'" & Round((cm - Int(cm / 30.48) *
30.48) / 2.54, 2) & """"

But I suspect that you will want further refinements that
nay make the expression so complex that it could be near
incomprehensible.
 
M

Marshall Barton

cozturk said:
before i try your new suggestion (cause it's almost 5:00am here and i'm with
no sleep yet), why do we declare inch As Currency??
Force of habit. Since Currency is a fixed point value (with
4 decimal places), I use Currency to reduce rounding errors
when values are converted to another type or string. You
can probably get a near identical result by using Double.
To follow my own advice, I should also use Currency
constants in the expression:
. . .
ft = Int(cms / 30.48@)
inch = Round((cms - ft * 30.48@) / 2.54@, 2)

Me.feet = ft "'" & inch & """"

Still, i believe i'd need to get the intiger part of the result in one
variable and change to string, then get the decimal part of the result and do
a calculation and save the final result as string again.
I must be confused about something because that's what I
think the above code does.

am i lost in the logic of things???
At this point, I not sure if it's you or me that's lost.
--
Marsh
MVP [MS Access]

Marshall Barton wrote: []
Based on your code the name should be cms:

Private Sub cms_AfterUpdate()
Dim ft As Integer, inch As Currency

ft = Int(cms / 30.48)
inch = Round((cms - ft * 30.48) / 2.54, 2)

Me.feet = ft "'" & inch & """"

End Sub
 
C

cozturk via AccessMonster.com

Marshall said:
before i try your new suggestion (cause it's almost 5:00am here and i'm with
no sleep yet), why do we declare inch As Currency??

Force of habit. Since Currency is a fixed point value (with
4 decimal places), I use Currency to reduce rounding errors
when values are converted to another type or string. You
can probably get a near identical result by using Double.
To follow my own advice, I should also use Currency
constants in the expression:
. . .
ft = Int(cms / 30.48@)
inch = Round((cms - ft * 30.48@) / 2.54@, 2)

Me.feet = ft "'" & inch & """"
Still, i believe i'd need to get the intiger part of the result in one
variable and change to string, then get the decimal part of the result and do
a calculation and save the final result as string again.

I must be confused about something because that's what I
think the above code does.
am i lost in the logic of things???

At this point, I not sure if it's you or me that's lost.
[]
Based on your code the name should be cms:
[quoted text clipped - 7 lines]

End Sub

I'm lost becouse I'm from a country that uses metric system and every
measurment is increments or decrements of 10.

Now here I can change a metric 180 cms to non-metric 5.906 feet, no problem
in that. But ı need to have it in 5 feet xx inch format that is the problem
 
D

Douglas J. Steele

Slight typo, Marsh. You left out an ampersand:

Me.feet = ft & "'" & inch & """"

And just a comment:

ft = Int(cms / 30.48)

could just as easily been

ft = cms \ 30.48



--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


Marshall Barton said:
cozturk said:
cozturk said:
Sorry, my brain got totally dead on this.

[quoted text clipped - 4 lines]
ft = Int(cm / 30.48)
in = Round((cm - ft * 30.48) / 2.54, 0)

Thanks so much for the quick response Marshal. However, I think I need
the
result as text as there will be ' for feet and " for inch.

your formula gives me "00"

for example on 180 cms which is 5.906 feet which is 5'11"
another could be 210 cms which is 6.89 feet which is 6'10"

also, do you think i have to write an after update or lost focus event??
i
just simply wrote
"=[kilos]/0,45" at the control source for "pounds" textbox which gives me
an
immediate conversion on kilos to pounds with no event procedures or
queries.


sorry, didn't write the code which gave me the "00" result:

Private Sub cms_AfterUpdate()

ft = Int(cm / 30.48)
inch = Round((cm - ft * 30.48) / 2.54, 2)

Me.feet = ft & inch

End Sub


Well, that code looks like there are a couple of things
going on. First, I suspect that the centimeters text box is
not named cm.

Second, you never declared the ft and inch variables (a poor
practice), which tells me that you do not have the
OPTION EXPLICIT
statement at the top of the module. This is a good practice
because you will be immediately notified when you use an
undeclared variable, especially when you misspell a name.
Based on your code the name should be cms:

Private Sub cms_AfterUpdate()
Dim ft As Integer, inch As Currency

ft = Int(cms / 30.48)
inch = Round((cms - ft * 30.48) / 2.54, 2)

Me.feet = ft "'" & inch & """"

End Sub


If you want to use a text box expression instead of VBA code
in an event procedure, try:

=Int(cm / 30.48) & "'" & Round((cm - Int(cm / 30.48) *
30.48) / 2.54, 2) & """"

But I suspect that you will want further refinements that
nay make the expression so complex that it could be near
incomprehensible.
 
J

Jan Kowalski

U¿ytkownik "cozturk via AccessMonster.com said:
Marshall said:
Sorry, my brain got totally dead on this.
[quoted text clipped - 6 lines]
convert the .34 to inches by multiplying it with 12 which will make 5
feet 4
inches.

ft = Int(cm / 30.48)
in = Round((cm - ft * 30.48) / 2.54, 0)

Thanks so much for the quick response Marshal. However, I think I need the
result as text as there will be ' for feet and " for inch.

your formula gives me "00"

for example on 180 cms which is 5.906 feet which is 5'11"
another could be 210 cms which is 6.89 feet which is 6'10"

also, do you think i have to write an after update or lost focus event?? i
just simply wrote
"=[kilos]/0,45" at the control source for "pounds" textbox which gives me
an
immediate conversion on kilos to pounds with no event procedures or
queries.
 
C

cozturk via AccessMonster.com

Duaglas, I've corrected the amp trying the code. Still gives 00
Slight typo, Marsh. You left out an ampersand:

Me.feet = ft & "'" & inch & """"

And just a comment:

ft = Int(cms / 30.48)

could just as easily been

ft = cms \ 30.48
[quoted text clipped - 61 lines]
nay make the expression so complex that it could be near
incomprehensible.
 
M

Marshall Barton

Douglas said:
Slight typo, Marsh. You left out an ampersand:

Me.feet = ft & "'" & inch & """"

And just a comment:

ft = Int(cms / 30.48)

could just as easily been

ft = cms \ 30.48


Thanks for spotting the missing & Doug

Re using \
I never use \ with non-integer operands because the operands
are converted to integers via the usual rounding BEFORE the
division is performed. e.g.
10 \ 2.5 is the same as CInt(10) \ CInt(2.5) and is = 5
 
M

Marshall Barton

cozturk said:
Marshall said:
before i try your new suggestion (cause it's almost 5:00am here and i'm with
no sleep yet), why do we declare inch As Currency??

Force of habit. Since Currency is a fixed point value (with
4 decimal places), I use Currency to reduce rounding errors
when values are converted to another type or string. You
can probably get a near identical result by using Double.
To follow my own advice, I should also use Currency
constants in the expression:
. . .
ft = Int(cms / 30.48@)
inch = Round((cms - ft * 30.48@) / 2.54@, 2)

Me.feet = ft "'" & inch & """"
Still, i believe i'd need to get the intiger part of the result in one
variable and change to string, then get the decimal part of the result and do
a calculation and save the final result as string again.

I must be confused about something because that's what I
think the above code does.
am i lost in the logic of things???

At this point, I not sure if it's you or me that's lost.
[]
Based on your code the name should be cms:
[quoted text clipped - 7 lines]

I'm lost becouse I'm from a country that uses metric system and every
measurment is increments or decrements of 10.

Now here I can change a metric 180 cms to non-metric 5.906 feet, no problem
in that. But ? need to have it in 5 feet xx inch format that is the problem


All the more reason to use a fixed point number type.

..1 does not convert to a precise floating point number. You
can see the (miniscule) error in this simple example in the
immediate window.

?1 - (.1 + .1 + .1 + .1 + .1 + .1 + .1 + .1 + .1 + .1)
-5.55111512312578E-17

But using currency is precise

?1-(.1@+.1@+.1@+.1@+.1@+.1@+.1@+.1@+.1@+.1@)
0

All that is probably unimportant to your question so lets
skip the floating/fixed point side discussion and use this:

Private Sub cms_AfterUpdate()
Dim ft As Integer, inch As Double

ft = Int(cms / 30.48)
inch = Round((cms - ft * 30.48) / 2.54, 2)

Me.feet = ft & "'" & inch & """"

End Sub

If you are still getting 0, there is something wrong with
the way you have followed my suggested code. You should
either get an error (because I made a typo) or you should
get something like 5'10.87"
 
C

cozturk via AccessMonster.com

YAY!!! This code worked...

Thanks so much Marshall..

Celal

Marshall said:
[quoted text clipped - 33 lines]
Now here I can change a metric 180 cms to non-metric 5.906 feet, no problem
in that. But ? need to have it in 5 feet xx inch format that is the problem

All the more reason to use a fixed point number type.

.1 does not convert to a precise floating point number. You
can see the (miniscule) error in this simple example in the
immediate window.

?1 - (.1 + .1 + .1 + .1 + .1 + .1 + .1 + .1 + .1 + .1)
-5.55111512312578E-17

But using currency is precise

?1-(.1@+.1@+.1@+.1@+.1@+.1@+.1@+.1@+.1@+.1@)
0

All that is probably unimportant to your question so lets
skip the floating/fixed point side discussion and use this:

Private Sub cms_AfterUpdate()
Dim ft As Integer, inch As Double

ft = Int(cms / 30.48)
inch = Round((cms - ft * 30.48) / 2.54, 2)

Me.feet = ft & "'" & inch & """"

End Sub

If you are still getting 0, there is something wrong with
the way you have followed my suggested code. You should
either get an error (because I made a typo) or you should
get something like 5'10.87"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top