Unit Converter

From Vectorlab
Jump to: navigation, search


The Universal Unit Soldier converts areas and volumes to units specified by the user as General Display units. By Petri Sakkinen

PROCEDURE UniversalUnitSoldier; { © Petri Sakkinen 2006 (29.11.2007) } 

{-----------------------------------------------------------------------------
The Universal Unit Soldier converts areas and volumes to units specified by the user as General Display units. 

VS returns all measurement in length unit -based units, eg. mm2 and mm3, 
which one does not want to use as output (tag, data record etc). 

This has not been tested with any other units than mm, m2 and m3, but should work with palms, square cubits and cubic fathoms just as well. 

I guess parts could (perhaps should) be functions, but procedures are more adaptable. 
-----------------------------------------------------------------------------}

VAR { global variables } 
	aText, vText, aUnitName, vUnitName : STRING; 
	aPrec, vPrec : LONGINT; { a: area, v: volume }
	theArea, theVolume, aFactor, vFactor : REAL; 

PROCEDURE TranslateUnit; 
VAR 
	inInch, inSqInch, inCuInch : REAL; 
BEGIN
	inInch    := GETPREFREAL(152);      { length units in inch - primary units } 
	inSqInch  := GETPREFREAL(176);      { area units in square inch } 
	aUnitName := GETPREFSTRING(178);    { area unit mark } 
	aPrec     := GETPREFLONGINT(179);   { precision of area } 
	inCuInch  := GETPREFREAL(180);      { volume units in cubic inch } 
	vUnitName := GETPREFSTRING(182);    { volume unit mark } 
	vPrec     := GETPREFLONGINT(183);   { precision of volume } 
	aFactor   := SQRT(1/inSqInch);      { area conversion } 
	aFactor   := (aFactor * inInch)^2;  { -- this we use elsewhere }
	vFactor   := (1/inCuInch)^(1/3);    { volume conversion } 
	vFactor   := (vFactor * inInch)^3;  { -- this we use elsewhere }
END;

{ example of use of calculated factors } 

PROCEDURE ConvertArea; { theArea & aFactor are passed from outside } 
BEGIN
	theArea := theArea/aFactor;
	aText := CONCAT(NUM2STR(aPrec, theArea), aUnitName);
END;

PROCEDURE ConvertVolume;  { theVolume & vFactor are passed from outside } 
BEGIN
	theVolume := theVolume/vFactor;
	vText := CONCAT(NUM2STR(vPrec, theVolume), vUnitName);
END;

BEGIN
	theArea   := 1000000;    { test value, eg 1000 x 1000 mm } 
	theVolume := 1000000000; { test value, eg 1000 x 1000 x 1000 mm }
	TranslateUnit;
	ConvertArea;
	ConvertVolume; 
	MESSAGE(CONCAT('Area: ', aText, ' - Volume: ', vText)); { testing, testing, one, two, three } 
END;

RUN(UniversalUnitSoldier);