(71B) Unit Conversions
04-13-2021, 04:00 PM
Post: #1
 Dave Britten Senior Member Posts: 2,293 Joined: Dec 2013
(71B) Unit Conversions
This is a simple unit conversion program built mainly using the conversion factors from the 48.

To use, RUN CONV, enter a value to convert, enter the abbreviation of the units to convert from, and then enter the abbreviation of the units to convert to. The converted value will be displayed, and also available in RES.

The program will make sure you don't attempt to convert across categories of units, e.g. feet to Newtons, and will display "INCONSISTENT UNITS" if you attempt this.

Example:

Convert 5 fl.oz. to mL:

RUN CONV
? 5
FROM? FLOZ
TO? ML
147.867647812 ML

Unit abbreviations should be entered in uppercase, with two exceptions: Greek mu is replaced with lowercase u in microns and microliters, and the speed of light is entered as lowercase c so as not to confuse it with Celsius. Temperatures are entered as a single character with no degree symbol.

To add more units, simply add additional program lines to check U$and set F accordingly. To find the value to use for F, convert 1 of the new unit to the base unit (the first entry in each group where F=1). So if you frequently use picoliters, add line 156 that sets F=1E-15 (1 pL converted to m^3) when U$="PL".

Entirely new categories of units can be added by setting C at the beginning of each category block. Or you can delete lines/categories you don't need to save memory.

Code is listed below, and a LIF file is attached. On-device program size is 2,414 bytes.

Code:
0001 DIM X,F,O,C,D,U$0010 INPUT X 0020 INPUT "FROM? ";U$ @ GOSUB 100 0021 IF C=0 THEN GOTO 90 0022 D=C @ X=(X-O)*F 0030 INPUT "TO? ";U$@ GOSUB 100 0031 IF C=0 THEN GOTO 90 0032 IF C<>D THEN DISP "INCONSISTENT UNITS" @ END 0040 X=X/F+O @ PRINT X;U$ @ END  0090 DISP "NOT FOUND" @ END  0100 O=0 @ C=1 @ REM LENGTH 0101 IF U$="M" THEN F=1 @ RETURN 0102 IF U$="CM" THEN F=.01 @ RETURN  0103 IF U$="MM" THEN F=.001 @ RETURN 0104 IF U$="KM" THEN F=1000 @ RETURN  0105 IF U$="IN" THEN F=.0254 @ RETURN 0106 IF U$="FT" THEN F=.3048 @ RETURN  0107 IF U$="MI" THEN F=1609.344 @ RETURN 0108 IF U$="YD" THEN F=.9144 @ RETURN  0109 IF U$="LYR" THEN F=9.46052840488E+15 @ RETURN 0110 IF U$="NMI" THEN F=1852 @ RETURN  0111 IF U$="FATH" THEN F=1.82880365761 @ RETURN 0112 IF U$="MIL" THEN F=.0000254 @ RETURN  0113 IF U$="u" THEN F=.000001 @ RETURN 0120 C=2 @ REM AREA 0121 IF U$="SQM" THEN F=1 @ RETURN  0122 IF U$="SQCM" THEN F=.0001 @ RETURN 0123 IF U$="SQYD" THEN F=.83612736 @ RETURN  0124 IF U$="SQFT" THEN F=.09290304 @ RETURN 0125 IF U$="SQIN" THEN F=.00064516 @ RETURN  0126 IF U$="SQKM" THEN F=1000000 @ RETURN 0127 IF U$="HA" THEN F=10000 @ RETURN  0128 IF U$="SQMI" THEN F=2589988.11034 @ RETURN 0129 IF U$="ACRE" THEN F=4046.87260987 @ RETURN  0140 C=3 @ REM VOLUME 0141 IF U$="M^3" THEN F=1 @ RETURN 0142 IF U$="CM^3" THEN F=.000001 @ RETURN  0143 IF U$="YD^3" THEN F=.764554857984 @ RETURN 0144 IF U$="FT^3" THEN F=.028316846592 @ RETURN  0145 IF U$="IN^3" THEN F=.000016387064 @ RETURN 0146 IF U$="L" THEN F=.001 @ RETURN  0147 IF U$="GAL" THEN F=.003785411784 @ RETURN 0148 IF U$="PT" THEN F=.000473176473 @ RETURN  0149 IF U$="QT" THEN F=.000946352946 @ RETURN 0150 IF U$="ML" THEN F=.000001 @ RETURN  0151 IF U$="CU" THEN F=.0002365882365 @ RETURN 0152 IF U$="FLOZ" THEN F=2.95735295625E-5 @ RETURN  0153 IF U$="TBSP" THEN F=1.47867647813E-5 @ RETURN 0154 IF U$="TSP" THEN F=4.92892159375E-6 @ RETURN  0155 IF U$="uL" THEN F=.000000001 @ RETURN 0160 C=4 @ REM MASS 0161 IF U$="KG" THEN F=1 @ RETURN  0162 IF U$="G" THEN F=.001 @ RETURN 0163 IF U$="MG" THEN F=.000001 @ RETURN  0164 IF U$="LB" THEN F=.45359237 @ RETURN 0165 IF U$="OZ" THEN F=.028349523125 @ RETURN  0166 IF U$="CT" THEN F=.0002 @ RETURN 0167 IF U$="ST" THEN F=6.35029 @ RETURN  0180 C=5 @ REM FORCE 0181 IF U$="N" THEN F=1 @ RETURN 0182 IF U$="LBF" THEN F=4.44822161526 @ RETURN  0200 C=6 @ REM ENERGY 0201 IF U$="J" THEN F=1 @ RETURN 0202 IF U$="ERG" THEN F=.0000001 @ RETURN  0203 IF U$="KCAL" THEN F=4186.8 @ RETURN 0204 IF U$="CAL" THEN F=4.1868 @ RETURN  0205 IF U$="FT*LBF" THEN F=1.35581794833 @ RETURN 0206 IF U$="THERM" THEN F=105506000 @ RETURN  0207 IF U$="MEV" THEN F=1.60219E-13 @ RETURN 0208 IF U$="EV" THEN F=1.60219E-19 @ RETURN  0209 IF U$="BTU" THEN F=1055.05585262 @ RETURN 0220 C=7 @ REM TEMPERATURE 0221 IF U$="C" THEN F=1 @ O=0 @ RETURN  0222 IF U$="F" THEN F=5/9 @ O=32 @ RETURN 0223 IF U$="K" THEN F=1 @ O=273.15 @ RETURN  0224 IF U$="R" THEN F=5/9 @ O=491.67 @ RETURN 0240 C=8 @ REM PRESSURE 0241 IF U$="PA" THEN F=1 @ RETURN  0242 IF U$="ATM" THEN F=101325 @ RETURN 0243 IF U$="BAR" THEN F=100000 @ RETURN  0244 IF U$="PSI" THEN F=6894.75729317 @ RETURN 0245 IF U$="TORR" OR U$="MMHG" THEN F=133.322368421 @ RETURN 0246 IF U$="INHG" THEN F=3386.38815789 @ RETURN  0247 IF U$="INH2O" THEN F=248.84 @ RETURN 0248 IF U$="CMH2O" THEN F=97.968503937 @ RETURN  0249 IF U$="KG/CM^2" THEN F=98066.5 @ RETURN 0260 C=9 @ REM SPEED 0261 IF U$="M/S" THEN F=1 @ RETURN  0262 IF U$="CM/S" THEN F=.01 @ RETURN 0263 IF U$="FT/S" THEN F=.3048 @ RETURN  0264 IF U$="KPH" THEN F=1/3.6 @ RETURN 0265 IF U$="MPH" THEN F=.44704 @ RETURN  0266 IF U$="KT" THEN F=.514444444445 @ RETURN 0267 IF U$="c" THEN F=299792458 @ RETURN  0268 IF U\$="LYR/HR" THEN F=2.62792455691E+12 @ RETURN  1000 C=0 @ RETURN

 « Next Oldest | Next Newest »

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