Hot Topic (More than 10 Replies) Converting inches or centimeters to feet & inches (Read 21802 times)
Infinity
Senior Member
Members
*****
Offline


Diagonally parked in a
parallel dimension

Posts: 1290
Location: Massachusetts
Joined: May 27th, 2005
Converting inches or centimeters to feet & inches
Aug 29th, 2006 at 4:22pm
Print Post Print Post  
OK, I'm probably the only person here who has use for this, but I feel like contributing.  My company moves heavy and large machinery, so we often need to convert specs that show measurements in inches only to feet and inches, or likewise with centimeters.  Here's the code I use in my Workorder application:

Elements used:  CM, Feet, Inches

First, the centimeters conversion, if needed:

** PROGRAMMING SECTION: [CM] [On Element Change] **
Code
Select All
// Converts Centimeters to Inches

Inches = CM * 0.393700787 



Then, change inches to feet and inches:

** PROGRAMMING SECTION: [LENGTH_INCHES] [On Element Change] **
Code
Select All
// Changes inches over 12 to extra feet and inches

var vExtraInches as Double
var vExtraFeet as Double
var vExtraFeetInches as Double

if Inches > 12 then
	{
	vExtraFeetInches = (Inches / 12)
	vExtraFeet = @Int(VExtraFeetInches)
	vExtraInches = (VExtraFeetInches - vExtraFeet)
	vExtraInches = (VExtraInches * 12)
	Feet = Feet + vExtraFeet
	Inches = vExtraInches
	}

IF Inches = 12
	THEN
	{
	Feet = Feet + 1
	Inches = 0
	} 

  

**
Captain Infinity
Back to top
IP Logged
 
Bob_Hansen
Senior Member
Members
*****
Offline


WOW, They have the Internet
on computers now!

Posts: 1861
Location: Salem, NH
Joined: Nov 24th, 2002
Re: Converting inches or centimeters to feet & inc
Reply #1 - Aug 29th, 2006 at 5:21pm
Print Post Print Post  
Thanks for the contribution Captain.

Have you tried using @Mod to get the same result for feet and inches?
Here is an untested sample:

Code
Select All
if Inches > 12 then 	{
     Feet += @Int(Inches/12)
     Inches = @Mod(Inches,12)
     } 

  



Bob Hansen
Sesame Database Manager Professional
Sensible Solutions Inc.
Salem, NH
603-898-8223
Skype ID = sensiblesolutions
Back to top
IP Logged
 
Infinity
Senior Member
Members
*****
Offline


Diagonally parked in a
parallel dimension

Posts: 1290
Location: Massachusetts
Joined: May 27th, 2005
Re: Converting inches or centimeters to feet & inc
Reply #2 - Aug 29th, 2006 at 5:47pm
Print Post Print Post  
Thanks, I'll give it a try!
  

**
Captain Infinity
Back to top
IP Logged
 
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: Converting inches or centimeters to feet & inc
Reply #3 - Aug 29th, 2006 at 5:58pm
Print Post Print Post  
Bob,
Do you mean?

Quote:
Feet += @Int(Inches/12) // Increment by the result


Or:

Quote:
Feet = @Int(Inches/12) // Assign the result
  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
Back to top
IP Logged
 
Infinity
Senior Member
Members
*****
Offline


Diagonally parked in a
parallel dimension

Posts: 1290
Location: Massachusetts
Joined: May 27th, 2005
Re: Converting inches or centimeters to feet & inc
Reply #4 - Aug 29th, 2006 at 6:10pm
Print Post Print Post  
I'm looking for "increment", and it works well.  Thanks Bob, that cleans up my code a lot!  (I do this calculation in 12 places, length-width-height for 4 possible machines.)
  

**
Captain Infinity
Back to top
IP Logged
 
Bob_Hansen
Senior Member
Members
*****
Offline


WOW, They have the Internet
on computers now!

Posts: 1861
Location: Salem, NH
Joined: Nov 24th, 2002
Re: Converting inches or centimeters to feet & inc
Reply #5 - Aug 29th, 2006 at 6:56pm
Print Post Print Post  
Cow:
I did mean increment from what I understood of his initial code. 
He was increasing Feet by his result.  But thanks for keeping me honest.

Captain:
If you are using this in multiple places, consider making it into a Sub Routine in Global Code that accepts CM as the input.
Sub Routine will convert CM to inches and then convert to Feet and Inches.

Element Programming will then consist of only one line calling the Sub Routine(CM)

  



Bob Hansen
Sesame Database Manager Professional
Sensible Solutions Inc.
Salem, NH
603-898-8223
Skype ID = sensiblesolutions
Back to top
IP Logged
 
Infinity
Senior Member
Members
*****
Offline


Diagonally parked in a
parallel dimension

Posts: 1290
Location: Massachusetts
Joined: May 27th, 2005
Re: Converting inches or centimeters to feet & inc
Reply #6 - Aug 30th, 2006 at 12:47pm
Print Post Print Post  
Quote:
Captain:
If you are using this in multiple places, consider making it into a Sub Routine in Global Code that accepts CM as the input.
Sub Routine will convert CM to inches and then convert to Feet and Inches.


I have thought about this, but the "Inches to Feet & Inches" calculations are needed when changes are made to 12 different "Inches" elements, and the results are placed in 24 different "Feet & Inches" elements.  The @Mod() code simplifies things greatly, but since I need a separate code line to specify the output elements for each calculation anyway, the addition of variables in a sub routine would actually add lines of code now.  I think.  Not sure, but I think so. 

[/quote]Element Programming will then consist of only one line calling the Sub Routine(CM) [/quote]
The CM conversion is actually only done in one spot.  I added it as a "utility" for my users.  They can then cut & paste the result if they need to.  It isn't needed very often.

Thanks for your help Bob!  I'm not really sure what @MOD() is doing, but whatever it is it's doing it well.
  

**
Captain Infinity
Back to top
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Converting inches or centimeters to feet & inc
Reply #7 - Aug 30th, 2006 at 1:25pm
Print Post Print Post  
Infinity wrote on Aug 30th, 2006 at 12:47pm:
I'm not really sure what @MOD() is doing, but whatever it is it's doing it well.


It's a good idea to look up unfamiliar functions in the Programming Manual to see what they do before using them in your application. Here is the entry for @MOD():

@MOD(x,y)
@MD(x,y)
Type: Mathematical
Parameters: x as Double, y as Double
Returns: Double

"Modulo." Returns the remainder after integer division of its x by y. If y is not zero,
@Mod(x, y) returns the remainder that results when x is divided by y. If y is zero, zero
is returned.

ModResult = @MOD(13, 5)

ModResult is set to 3 because 13 / 5 = 2 with a remainder of 3. x and y can be form
elements or variables containing numbers or expressions that evaluate to numbers.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
Infinity
Senior Member
Members
*****
Offline


Diagonally parked in a
parallel dimension

Posts: 1290
Location: Massachusetts
Joined: May 27th, 2005
Re: Converting inches or centimeters to feet & inc
Reply #8 - Aug 30th, 2006 at 1:30pm
Print Post Print Post  
Thank you, Erika.  I did read page 188 before using it, I really did.  I didn't understand it, but I read it. Smiley

It's the math that throws me.  I'm not very good at math.  X's and Y's, integers and remainders...it's not quite Greek but it's close.  Sorry.
  

**
Captain Infinity
Back to top
IP Logged
 
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: Converting inches or centimeters to feet & inc
Reply #9 - Aug 30th, 2006 at 2:05pm
Print Post Print Post  
An integer is a "whole number", that is to say a number that does not, and cannot have a decimal (fractional) portion. So 1, 2, and 3 are integers. 1.8, 2.3, and 3.9 are not.

@Mod returns the integer remainder of an integer division. So if we divide 13 by 6, the integer answer to the division is 2, because 6 goes into 13 twice. But, because these are integers, there is a little left over:

6 * 2 = 12, not 13. That means there is a remainder of 1 (13 - (2 * 6)) = 1.

@Mod returns that remainder, if any.
  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
Back to top
IP Logged
 
Infinity
Senior Member
Members
*****
Offline


Diagonally parked in a
parallel dimension

Posts: 1290
Location: Massachusetts
Joined: May 27th, 2005
Re: Converting inches or centimeters to feet & inc
Reply #10 - Aug 30th, 2006 at 2:20pm
Print Post Print Post  
Thank you.
  

**
Captain Infinity
Back to top
IP Logged