Post Reply 
CONVERT and variables for arguments
10-22-2015, 11:35 AM (This post was last modified: 10-27-2015 11:54 AM by epp.)
Post: #22
Conversions Implemented as a Function
Thought I'd re-invent the wheel again. Listed below is a function to convert, CNV(X), where X can be a real (20) or a unit (20_cm). Two unit conversions, for distance and volume, and a driver to do the selection are included. Usage:

RPN:
20 ENTER
CNV

TEXTBOOK:
CNV(20)

If the original number you are converting is in units the answer will be in terms of units. For example, when converting 18 from inches to feet we have

18_inch -> 1.5_ft
18 -> 1.5

The following code can easily be extended to include other data types.

Code:

Distance();
Volume();

EXPORT CnvApp;
EXPORT CNV(x)
begin
  local t = type(x);
  if (t <> 0) and (t <> 9) then msgbox("Input must be REAL or UNIT"); return x; end;
  choose(CnvApp, "convert", {"Distance", "Volume", "Weight", "Temperature"} );
  if (CnvApp == 1) then return Distance(x, t); end;
  if (CnvApp == 2) then return Volume(x, t); end;
end;

EXPORT CnvDistFrom,CnvDistTo;
Distance(x, t)
begin
  local menu,unit;
  unit := {1_mm, 1_cm, 1_m, 1_km, 1_inch, 1_ft, 1_yd, 1_mile};
  menu := {"mm", "cm", "m", "km", "inches", "feet", "yards", "miles"};
  if (t == 0) then
    choose(CnvDistFrom, "From", menu);
    x := x * unit[CnvDistFrom];
  end;
  choose(CnvDistTo, "To", menu);
  x := convert(x, unit[CnvDistTo]);
  if (t == 0) then x := x / unit[CnvDistTo]; else x; end;
end;

EXPORT CnvVolFrom,CnvVolTo;
Volume(x, t)
begin
  local menu,unit;
  unit := {1_ml, 1_l, 1_tsp, 1_tbsp, 1_qt};
  menu := {"ml", "liter", "tsp", "tbsp", "qt"};
  if (t == 0) then
    choose(CnvVolFrom, "From", menu);
    x := x * unit[CnvVolFrom];
  end;
  choose(CnvVolTo, "To", menu);
  x := convert(x, unit[CnvVolTo]);
  if (t == 0) then x := x / unit[CnvVolTo]; else x; end;
end;

To place CNV on the command line choose

Toolbox > User > CNV > CNV

If you have previously visited this menu then choose Toolbox and keep pressing ENTER. Previous choices are saved by both the PRIME and the CNV program.

Alternatively you can bind a key to insert "CNV" and then choose

Shift > Help > KEY

where KEY is the one you bind to CNV. The following code binds the Units key to CNV.

Code:

KEY K_Templ()
BEGIN
RETURN "CNV";
END;

On a small screen selecting menu items while running CNV can be a challenge. Instead use the rocker to navigate to the selection or use the keyboard and choose a number that corresponds to the selection.
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
CONVERT and variables for arguments - epp - 10-15-2015, 06:16 PM
Conversions Implemented as a Function - epp - 10-22-2015 11:35 AM



User(s) browsing this thread: 1 Guest(s)