PDA

View Full Version : Metric Converter

Chompy
06-24-2007, 08:39 PM
Well, I got bored and suddenly got this idea of making a metric converter... o.O

Convertion Types:
Weight
Length
Pressure

Weight
kilograms
pounds
grams

Length
meter
centimeters
miles
feet
inches
yards

Pressure
bar
kg
cm
psi

And the function..

function convertMetric(value, from, too) {
if (from == too) {
return value;
}
temp.category = new[2];
temp.weight = {"kilograms", "pounds", "grams"};
temp.length = {"meter", "centimeters", "miles", "feet", "inches", "yards"};
temp.pressure = {"bar", "kg", "cm", "psi"};

if (weight.index(@ from) >= 0) category[0] = "weight";
else if (length.index(@ from) >= 0) category[0] = "length";
else if (pressure.index(@ from) >= 0) category[0] = "pressure";

if (weight.index(@ too) >= 0) category[1] = "weight";
else if (length.index(@ too) >= 0) category[1] = "length";
else if (pressure.index(@ too) >= 0) category[1] = "pressure";

if (category[0] != category[1]) {
return -1;
}
temp.var = value;
// Create metric chart with wanted value
// CHART START
temp.metric = new TStaticVar();
// Weight Conversion
metric.kilograms_pounds = (var * 2.2046);
metric.kilograms_grams = (var * 453.53);
metric.pounds_kilograms = (var * 0.4536);
metric.pounds_grams = (var * 453.59);
metric.grams_kilograms = (var * 0.001);
metric.grams_pounds = (var * 0.0022);
// Length Conversion
metric.meter_centimeters = (var * 100);
metric.meter_miles = (var * 0.00062);
metroc.meter_feet = (var * 3.28084);
metric.meter_inches = (var * 39.3701);
metric.meter_yards = (var * 1.09361);
metric.centimeters_meter = (var * 0.01000);
metric.centimeters_miles = (var * 0.00001);
metric.centimeters_feet = (var * 0.03281);
metric.centimeters_inches = (var * 0.39370);
metric.centimeters_yards = (var * 0.01094);
metric.miles_meter = (var * 1609.34);
metric.miles_centimeters = (var * 160934);
metric.miles_feet = (var * 5280);
metric.miles_inches = (var * 63360);
metric.miles_yards = (var * 1760);
metric.feet_meter = (var * 0.30480);
metric.feet_centimeters = (var * 30.4800);
metric.feet_miles = (var * 0.00019);
metric.feet_inches = (var * 12);
metric.feet_yards = (var * 0.33333);
metric.inches_meter = (var * 0.02540);
metric.inches_centimeters = (var * 2.54000);
metric.inches_miles = (var * 0.00002);
metric.inches_feet = (var * 0.08333);
metric.inches_yards = (var * 0.02778);
metric.yards_meter = (var * 0.91440);
metric.yards_centimeters = (var * 91.4400);
metric.yards_miles = (var * 0.00057);
metric.yards_feet = (var * 3);
metric.yards_inches = (var * 36);
// Pressure Conversion
metric.bar_kg = (var * 1.0197);
metric.bar_cm = (var * 1.0197);
metric.bar_psi = (var * 14.500);
metric.kg_bar = (var * 0.9807);
metric.kg_cm = (var * 1);
metric.kg_psi = (var * 14.22);
metric.cm_bar = (var * 0.9807);
metric.cm_kg = (var * 1);
metric.cm_psi = (var * 14.22);
metric.psi_bar = (var * 0.0690);
metric.psi_kg = (var * 0.0703);
metric.psi_cm = (var * 0.0703);
return metric.(@ from @ "_" @ too);
}

and Celsius <-> Fahreinheit

public function celstiusToFahrenheit(c) {
return (9/5)*(c+32);
}
public function fahrenheitToCelsius(f) {
return (5/9)*(f-32);
}

Example of use:

function onCreated() {
temp.value = convertMetric(8, "feet", "yards");
echo(value);
}

would output
2.66664

Note
I was bored when I made this, will probably improve it later..
Oh, and feel free to post how I can improve it later or any bugs/wrong convertions

Inverness
06-24-2007, 10:33 PM
That takes a lot of boredom to make :O.
Weirdo :D, I mean that in a good way.

Chompy
06-24-2007, 10:46 PM
That takes a lot of boredom to make :O.
Weirdo :D, I mean that in a good way.

<3, thanks :p

coreys
06-25-2007, 12:24 AM
Awesome, Chompy :)

Chompy
06-25-2007, 03:46 AM
Awesome, Chompy :)

thanks