Useful method.

Forums for specific tips, techniques and example code
ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Useful method.

Postby ConvertFromOldNGs » Fri Aug 07, 2009 2:24 pm

by Carl Ranson >> Tue, 9 May 2000 0:10:14 GMT

I don't know if this will be terribly useful to anyone, but since it was a pain to test and debug I thought I'ld post it anyway.

Its a method I wrote on real that will convert between lengths of different units.

eg
1.convertLength('ft', 'mm'); // gives equiv. of 1 foot in mm.
2.convertLength('in', 'cm'); // gives equiv. of 2 inches in cm.

The units of "q" is quarter-inches.

regards,
CR

convertLength(unit : String; targetUnit : String) : Real;
// convert a length in m, cm, mm, in, ft, q to another unit
vars
value : Real;
realUnit : String;begin

if unit = targetUnit then
return self;
else
value := self;
realUnit := unit;
if unit = 'ft' then
realUnit := 'in';
value := value * 12;
elseif unit = 'q' then
realUnit := 'in';
value := value / 4;
elseif unit = 'm' then
realUnit := 'mm';
value := value * 1000;
elseif unit = 'cm' then
realUnit := 'mm';
value := value * 10;
endif;

if realUnit = 'mm' then
if targetUnit = 'm' then
return value / 1000;
elseif targetUnit = 'cm' then
return value / 10;
elseif targetUnit = 'mm' then
return value;
elseif targetUnit = 'in' then
return value / 0.0254 / 1000;
elseif targetUnit = 'ft' then
return value / (0.0254*12)/1000;
elseif targetUnit = 'q' then
return value / (0.0254/4)/1000;
endif;
elseif realUnit = 'in' then
if targetUnit = 'm' then
return value * 25.4 /1000;
elseif targetUnit = 'cm' then
return value * 25.4 / 10;
elseif targetUnit = 'mm' then
return value * 25.4;
elseif targetUnit = 'ft' then
return value / 12;
elseif targetUnit = 'q' then
return value * 4;
elseif targetUnit = 'in' then
return value;
endif;
endif;
endif;

// shouldn't arrive here for valid inputs
return null;
end;

Return to “Tips and Techniques”

Who is online

Users browsing this forum: Bing [Bot] and 24 guests