internal static string ConvertVolume(string from, string to, float input) { string answer; float convertedValue = 0.0F; string abbreviatedFromUnit; string abbreviatedToUnit; SharedMethods sm = new SharedMethods(); if (from != null && to != null) { #region Convert from Cups //from cups if (from.Trim() == "Cups") { if (to.Trim() == "Fluid Ounces") { convertedValue = input * 8F; goto Answered; } else if (to.Trim() == "Gallons") { convertedValue = input * 0.0625F; goto Answered; } else if (to.Trim() == "Liters") { convertedValue = input * 0.236588F; goto Answered; } else if (to.Trim() == "Milliliters") { convertedValue = input * 236.588F; goto Answered; } else if (to.Trim() == "Pints") { convertedValue = input * 0.5F; goto Answered; } else if (to.Trim() == "Quarts") { convertedValue = input * 0.25F; goto Answered; } else if (to.Trim() == "Tablespoons") { convertedValue = input * 16F; goto Answered; } else if (to.Trim() == "Teaspoons") { convertedValue = input * 48F; goto Answered; } else { convertedValue = input; goto Answered; } } #endregion #region Convert from Fluid Ounces //from fluid ounces if (from.Trim() == "Fluid Ounces") { if (to.Trim() == "Cups") { convertedValue = input * 0.125F; goto Answered; } else if (to.Trim() == "Gallons") { convertedValue = input * 0.0078125F; goto Answered; } else if (to.Trim() == "Liters") { convertedValue = input * 0.0295735F; goto Answered; } else if (to.Trim() == "Milliliters") { convertedValue = input * 29.5735F; goto Answered; } else if (to.Trim() == "Pints") { convertedValue = input * 0.0625F; goto Answered; } else if (to.Trim() == "Quarts") { convertedValue = input * 0.03125F; goto Answered; } else if (to.Trim() == "Tablespoons") { convertedValue = input * 2F; goto Answered; } else if (to.Trim() == "Teaspoons") { convertedValue = input * 6F; goto Answered; } else { convertedValue = input; goto Answered; } } #endregion #region Convert from Gallons //from gallons if (from.Trim() == "Gallons") { if (to.Trim() == "Cups") { convertedValue = input * 16F; goto Answered; } else if (to.Trim() == "Fluid Ounces") { convertedValue = input * 128F; goto Answered; } else if (to.Trim() == "Liters") { convertedValue = input * 3.78541F; goto Answered; } else if (to.Trim() == "Milliliters") { convertedValue = input * 3785.41F; goto Answered; } else if (to.Trim() == "Pints") { convertedValue = input * 8F; goto Answered; } else if (to.Trim() == "Quarts") { convertedValue = input * 4F; goto Answered; } else if (to.Trim() == "Tablespoons") { convertedValue = input * 256F; goto Answered; } else if (to.Trim() == "Teaspoons") { convertedValue = input * 768F; goto Answered; } else { convertedValue = input; goto Answered; } } #endregion #region Convert from Liters //from liters if (from.Trim() == "Liters") { if (to.Trim() == "Cups") { convertedValue = input * 4.22675F; goto Answered; } else if (to.Trim() == "Fluid Ounces") { convertedValue = input * 33.814F; goto Answered; } else if (to.Trim() == "Gallons") { convertedValue = input * 0.264172F; goto Answered; } else if (to.Trim() == "Milliliters") { convertedValue = input * 1000F; goto Answered; } else if (to.Trim() == "Pints") { convertedValue = input * 2.11338F; goto Answered; } else if (to.Trim() == "Quarts") { convertedValue = input * 1.05669F; goto Answered; } else if (to.Trim() == "Tablespoons") { convertedValue = input * 67.628F; goto Answered; } else if (to.Trim() == "Teaspoons") { convertedValue = input * 202.884F; goto Answered; } else { convertedValue = input; goto Answered; } } #endregion #region Convert from Milliliters //from milliliters if (from.Trim() == "Milliliters") { if (to.Trim() == "Cups") { convertedValue = input * 0.00422675F; goto Answered; } else if (to.Trim() == "Fluid Ounces") { convertedValue = input * 0.033814F; goto Answered; } else if (to.Trim() == "Gallons") { convertedValue = input * 0.000264172F; goto Answered; } else if (to.Trim() == "Liters") { convertedValue = input * 0.001F; goto Answered; } else if (to.Trim() == "Pints") { convertedValue = input * 0.00211338F; goto Answered; } else if (to.Trim() == "Quarts") { convertedValue = input * 0.00105669F; goto Answered; } else if (to.Trim() == "Tablespoons") { convertedValue = input * 0.067628F; goto Answered; } else if (to.Trim() == "Teaspoons") { convertedValue = input * 0.202884F; goto Answered; } else { convertedValue = input; goto Answered; } } #endregion #region Convert from Pints if (from.Trim() == "Pints") { if (to.Trim() == "Cups") { convertedValue = input * 2F; goto Answered; } else if (to.Trim() == "Fluid Ounces") { convertedValue = input * 16F; goto Answered; } else if (to.Trim() == "Gallons") { convertedValue = input * 0.125F; goto Answered; } else if (to.Trim() == "Liters") { convertedValue = input * 0.473176F; goto Answered; } else if (to.Trim() == "Milliliters") { convertedValue = input * 473.176F; goto Answered; } else if (to.Trim() == "Quarts") { convertedValue = input * 0.5F; goto Answered; } else if (to.Trim() == "Tablespoons") { convertedValue = input * 32F; goto Answered; } else if (to.Trim() == "Teaspoons") { convertedValue = input * 96F; goto Answered; } else { convertedValue = input; goto Answered; } } #endregion #region Convert from Quarts if (from.Trim() == "Quarts") { if (to.Trim() == "Cups") { convertedValue = input * 4F; goto Answered; } else if (to.Trim() == "Fluid Ounces") { convertedValue = input * 32F; goto Answered; } else if (to.Trim() == "Gallons") { convertedValue = input * 0.25F; goto Answered; } else if (to.Trim() == "Liters") { convertedValue = input * 0.946353F; goto Answered; } else if (to.Trim() == "Milliliters") { convertedValue = input * 946.353F; goto Answered; } else if (to.Trim() == "Pints") { convertedValue = input * 2F; goto Answered; } else if (to.Trim() == "Tablespoons") { convertedValue = input * 64F; goto Answered; } else if (to.Trim() == "Teaspoons") { convertedValue = input * 192F; goto Answered; } else { convertedValue = input; goto Answered; } } #endregion #region Convert from Tablespoons if (from.Trim() == "Tablespoons") { if (to.Trim() == "Cups") { convertedValue = input * 0.0625F; goto Answered; } else if (to.Trim() == "Fluid Ounces") { convertedValue = input * 0.5F; goto Answered; } else if (to.Trim() == "Gallons") { convertedValue = input * 0.00390625F; goto Answered; } else if (to.Trim() == "Liters") { convertedValue = input * 0.0147868F; goto Answered; } else if (to.Trim() == "Milliliters") { convertedValue = input * 14.7868F; goto Answered; } else if (to.Trim() == "Pints") { convertedValue = input * 0.03125F; goto Answered; } else if (to.Trim() == "Quarts") { convertedValue = input * 0.015625F; goto Answered; } else if (to.Trim() == "Teaspoons") { convertedValue = input * 3F; goto Answered; } else { convertedValue = input; goto Answered; } } #endregion #region Convert from Teaspoons if (from.Trim() == "Teaspoons") { if (to.Trim() == "Cups") { convertedValue = input * 0.0208333F; goto Answered; } else if (to.Trim() == "Fluid Ounces") { convertedValue = input * 0.5F; goto Answered; } else if (to.Trim() == "Gallons") { convertedValue = input * 0.00130208F; goto Answered; } else if (to.Trim() == "Liters") { convertedValue = input * 0.00492892F; goto Answered; } else if (to.Trim() == "Milliliters") { convertedValue = input * 4.92892F; goto Answered; } else if (to.Trim() == "Pints") { convertedValue = input * 0.0104167F; goto Answered; } else if (to.Trim() == "Quarts") { convertedValue = input * 0.00520833F; goto Answered; } else if (to.Trim() == "Tablespoons") { convertedValue = input * 0.333333F; goto Answered; } else { convertedValue = input; goto Answered; } } #endregion } else { return ""; } Answered: ShortenUnitsForDisplay abbreviateFrom = new ShortenUnitsForDisplay(from); ShortenUnitsForDisplay abbreviateTo = new ShortenUnitsForDisplay(to); abbreviatedFromUnit = abbreviateFrom.ToString(); abbreviatedToUnit = abbreviateTo.ToString(); string fromAnswerForDisplay, toAnswerForDisplay; sm.RemoveTrailingZeros(input, convertedValue, out fromAnswerForDisplay, out toAnswerForDisplay); answer = System.String.Format("{0} {1}\n =\n{2} {3}", fromAnswerForDisplay, abbreviatedFromUnit, toAnswerForDisplay, abbreviatedToUnit); return answer; }
internal static string ConvertSpeed(string from, string to, float input) { string answer; float convertedValue = 0.0F; string abbreviatedFromUnit; string abbreviatedToUnit; SharedMethods sm = new SharedMethods(); if (from != null && to != null) { if (from != null && to != null) { #region Convert from MPH if (from.Trim() == "Miles/Hour") { if (to.Trim() == "Kilometers/Hour") { convertedValue = input * 1.60934F; goto Answered; } else if (to.Trim() == "Feet/Second") { convertedValue = input * 1.46667F; goto Answered; } else if (to.Trim() == "Meters/Second") { convertedValue = input * 0.44704F; goto Answered; } else { convertedValue = input; goto Answered; } } #endregion #region Convert from KPH if (from.Trim() == "Kilometers/Hour") { if (to.Trim() == "Miles/Hour") { convertedValue = input * 0.621371F; goto Answered; } else if (to.Trim() == "Feet/Second") { convertedValue = input * 0.911344F; goto Answered; } else if (to.Trim() == "Meters/Second") { convertedValue = input * 0.277778F; goto Answered; } else { convertedValue = input; goto Answered; } } #endregion #region Convert from FPS if (from.Trim() == "Feet/Second") { if (to.Trim() == "Miles/Hour") { convertedValue = input * 0.681818F; goto Answered; } else if (to.Trim() == "Kilometers/Hour") { convertedValue = input * 1.09728F; goto Answered; } else if (to.Trim() == "Meters/Second") { convertedValue = input * 0.3048F; goto Answered; } else { convertedValue = input; goto Answered; } } #endregion #region Convert from MPS if (from.Trim() == "Meters/Second") { if (to.Trim() == "Miles/Hour") { convertedValue = input * 2.23694F; goto Answered; } else if (to.Trim() == "Kilometers/Hour") { convertedValue = input * 3.6F; goto Answered; } else if (to.Trim() == "Feet/Second") { convertedValue = input * 3.28084F; goto Answered; } else { convertedValue = input; goto Answered; } } #endregion } } else { return ""; } Answered: ShortenUnitsForDisplay abbreviateFrom = new ShortenUnitsForDisplay(from); ShortenUnitsForDisplay abbreviateTo = new ShortenUnitsForDisplay(to); abbreviatedFromUnit = abbreviateFrom.ToString(); abbreviatedToUnit = abbreviateTo.ToString(); string fromAnswerForDisplay, toAnswerForDisplay; sm.RemoveTrailingZeros(input, convertedValue, out fromAnswerForDisplay, out toAnswerForDisplay); answer = System.String.Format("{0} {1}\n =\n{2} {3}", fromAnswerForDisplay, abbreviatedFromUnit, toAnswerForDisplay, abbreviatedToUnit); return answer; }
//Math for the conversions. internal static string ConvertWeight(string from, string to, float input) { string answer; float convertedValue = 0.0F; string abbreviatedFromUnit; string abbreviatedToUnit; SharedMethods sm = new SharedMethods(); //If both a From and To unit have been selected, convert the value in the textbox and display the result. if (from != null && to != null) { //From pounds. if (from.Trim() == "Pounds") { if (to.Trim() == "Kilograms") { convertedValue = input * 0.453592F; goto Answered; } else if (to.Trim() == "Tons") { convertedValue = input * 0.0005F; goto Answered; } else if (to.Trim() == "Grams") { convertedValue = input * 453.592F; goto Answered; } else if (to.Trim() == "Milligrams") { convertedValue = input * 453592F; goto Answered; } else if (to.Trim() == "Ounces") { convertedValue = input * 16F; goto Answered; } else { convertedValue = input; goto Answered; } } //From kilograms. else if (from.Trim() == "Kilograms") { if (to.Trim() == "Pounds") { convertedValue = input * 2.20462F; goto Answered; } else if (to.Trim() == "Tons") { convertedValue = input * 0.00110231F; goto Answered; } else if (to.Trim() == "Grams") { convertedValue = input * 1000F; goto Answered; } else if (to.Trim() == "Milligrams") { convertedValue = input * 1000000F; goto Answered; } else if (to.Trim() == "Ounces") { convertedValue = input * 35.274F; goto Answered; } else { convertedValue = input; goto Answered; } } //From tons. else if (from.Trim() == "Tons") { if (to.Trim() == "Pounds") { convertedValue = input * 2000F; goto Answered; } else if (to.Trim() == "Kilograms") { convertedValue = input * 907.185F; goto Answered; } else if (to.Trim() == "Grams") { convertedValue = input * 907185F; goto Answered; } else if (to.Trim() == "Milligrams") { convertedValue = input * 907200000F; goto Answered; } else if (to.Trim() == "Ounces") { convertedValue = input * 32000F; goto Answered; } else { convertedValue = input; goto Answered; } } //From grams. else if (from.Trim() == "Grams") { if (to.Trim() == "Pounds") { convertedValue = input * 0.00220462F; goto Answered; } else if (to.Trim() == "Kilograms") { convertedValue = input * 0.001F; goto Answered; } else if (to.Trim() == "Tons") { convertedValue = input * .0000011023F; goto Answered; } else if (to.Trim() == "Milligrams") { convertedValue = input * 1000F; goto Answered; } else if (to.Trim() == "Ounces") { convertedValue = input * 0.035274F; goto Answered; } else { convertedValue = input; goto Answered; } } //From milligrams. else if (from.Trim() == "Milligrams") { if (to.Trim() == "Pounds") { convertedValue = input * .0000022046F; goto Answered; } else if (to.Trim() == "Kilograms") { convertedValue = input * .000001F; goto Answered; } else if (to.Trim() == "Tons") { convertedValue = input * .0000000011023F; goto Answered; } else if (to.Trim() == "Grams") { convertedValue = input * .001F; goto Answered; } else if (to.Trim() == "Ounces") { convertedValue = input * .000035274F; goto Answered; } else { convertedValue = input; goto Answered; } } //From ounces. else if (from.Trim() == "Ounces") { if (to.Trim() == "Pounds") { convertedValue = input * .0625F; goto Answered; } else if (to.Trim() == "Kilograms") { convertedValue = input * .0283495F; goto Answered; } else if (to.Trim() == "Tons") { convertedValue = input * .00003125F; goto Answered; } else if (to.Trim() == "Milligrams") { convertedValue = input * 28349.5F; goto Answered; } else if (to.Trim() == "Grams") { convertedValue = input * 28.3495F; goto Answered; } else { convertedValue = input; goto Answered; } } } else { return ""; } Answered: ShortenUnitsForDisplay abbreviateFrom = new ShortenUnitsForDisplay(from); ShortenUnitsForDisplay abbreviateTo = new ShortenUnitsForDisplay(to); abbreviatedFromUnit = abbreviateFrom.ToString(); abbreviatedToUnit = abbreviateTo.ToString(); string fromAnswerForDisplay, toAnswerForDisplay; sm.RemoveTrailingZeros(input, convertedValue, out fromAnswerForDisplay, out toAnswerForDisplay); answer = System.String.Format("{0} {1}\n =\n{2} {3}", fromAnswerForDisplay, abbreviatedFromUnit, toAnswerForDisplay, abbreviatedToUnit); return answer; }
internal static string ConvertLength(string from, string to, float input) { string answer; float convertedValue = 0.0F; string abbreviatedFromUnit; string abbreviatedToUnit; SharedMethods sm = new SharedMethods(); if(from != null && to != null) { #region Convert from Centimeters if (from.Trim() == "Centimeters") { if (to.Trim() == "Kilometers") { convertedValue = input * 0.00001F; goto Answered; } else if (to.Trim() == "Meters") { convertedValue = input * 0.01F; goto Answered; } else if (to.Trim() == "Millimeters") { convertedValue = input * 10F; goto Answered; } else if (to.Trim() == "Miles") { convertedValue = input * 0.0000062137F; goto Answered; } else if (to.Trim() == "Yards") { convertedValue = input * 0.0109361F; goto Answered; } else if (to.Trim() == "Feet") { convertedValue = input * 0.0328084F; goto Answered; } else if (to.Trim() == "Inches") { convertedValue = input * 0.393701F; goto Answered; } else { convertedValue = input; goto Answered; } } #endregion #region Convert from Kilometers if (from.Trim() == "Kilometers") { if (to.Trim() == "Centimeters") { convertedValue = input * 100000F; goto Answered; } else if (to.Trim() == "Meters") { convertedValue = input * 1000F; goto Answered; } else if (to.Trim() == "Millimeters") { convertedValue = input * 1000000F; goto Answered; } else if (to.Trim() == "Miles") { convertedValue = input * 0.621371F; goto Answered; } else if (to.Trim() == "Yards") { convertedValue = input * 1093.61F; goto Answered; } else if (to.Trim() == "Feet") { convertedValue = input * 3280.84F; goto Answered; } else if (to.Trim() == "Inches") { convertedValue = input * 39370.1F; goto Answered; } else { convertedValue = input; goto Answered; } } #endregion #region Convert from Meters if (from.Trim() == "Meters") { if (to.Trim() == "Centimeters") { convertedValue = input * 100F; goto Answered; } else if (to.Trim() == "Kilometers") { convertedValue = input * 0.001F; goto Answered; } else if (to.Trim() == "Millimeters") { convertedValue = input * 1000F; goto Answered; } else if (to.Trim() == "Miles") { convertedValue = input * 0.000621371F; goto Answered; } else if (to.Trim() == "Yards") { convertedValue = input * 1.09361F; goto Answered; } else if (to.Trim() == "Feet") { convertedValue = input * 3.28084F; goto Answered; } else if (to.Trim() == "Inches") { convertedValue = input * 39.3701F; goto Answered; } else { convertedValue = input; goto Answered; } } #endregion #region Convert from Millimeters if (from.Trim() == "Millimeters") { if (to.Trim() == "Centimeters") { convertedValue = input * 0.1F; goto Answered; } else if (to.Trim() == "Kilometers") { convertedValue = input * 0.000001F; goto Answered; } else if (to.Trim() == "Meters") { convertedValue = input * 0.001F; goto Answered; } else if (to.Trim() == "Miles") { convertedValue = input * 0.00000062137F; goto Answered; } else if (to.Trim() == "Yards") { convertedValue = input * 0.00109361F; goto Answered; } else if (to.Trim() == "Feet") { convertedValue = input * 0.00328084F; goto Answered; } else if (to.Trim() == "Inches") { convertedValue = input * 0.0393701F; goto Answered; } else { convertedValue = input; goto Answered; } } #endregion #region Convert from Miles if (from.Trim() == "Miles") { if (to.Trim() == "Centimeters") { convertedValue = input * 160934F; goto Answered; } else if (to.Trim() == "Kilometers") { convertedValue = input * 1.60934F; goto Answered; } else if (to.Trim() == "Meters") { convertedValue = input * 1609.34F; goto Answered; } else if (to.Trim() == "Millimeters") { convertedValue = input * 1609000F; goto Answered; } else if (to.Trim() == "Yards") { convertedValue = input * 1760F; goto Answered; } else if (to.Trim() == "Feet") { convertedValue = input * 5280F; goto Answered; } else if (to.Trim() == "Inches") { convertedValue = input * 63360F; goto Answered; } else { convertedValue = input; goto Answered; } } #endregion #region Convert from Yards if (from.Trim() == "Yards") { if (to.Trim() == "Centimeters") { convertedValue = input * 91.44F; goto Answered; } else if (to.Trim() == "Kilometers") { convertedValue = input * 0.0009144F; goto Answered; } else if (to.Trim() == "Meters") { convertedValue = input * 0.9144F; goto Answered; } else if (to.Trim() == "Millimeters") { convertedValue = input * 914.4F; goto Answered; } else if (to.Trim() == "Miles") { convertedValue = input * 0.000568182F; goto Answered; } else if (to.Trim() == "Feet") { convertedValue = input * 3F; goto Answered; } else if (to.Trim() == "Inches") { convertedValue = input * 36F; goto Answered; } else { convertedValue = input; goto Answered; } } #endregion #region Convert from Feet if (from.Trim() == "Feet") { if (to.Trim() == "Centimeters") { convertedValue = input * 30.48F; goto Answered; } else if (to.Trim() == "Kilometers") { convertedValue = input * 0.0003048F; goto Answered; } else if (to.Trim() == "Meters") { convertedValue = input * 0.3048F; goto Answered; } else if (to.Trim() == "Millimeters") { convertedValue = input * 304.8F; goto Answered; } else if (to.Trim() == "Miles") { convertedValue = input * 0.000189394F; goto Answered; } else if (to.Trim() == "Yards") { convertedValue = input * 0.333333F; goto Answered; } else if (to.Trim() == "Inches") { convertedValue = input * 12F; goto Answered; } else { convertedValue = input; goto Answered; } } #endregion #region Convert from Inches if (from.Trim() == "Inches") { if (to.Trim() == "Centimeters") { convertedValue = input * 2.54F; goto Answered; } else if (to.Trim() == "Kilometers") { convertedValue = input * 0.0000254F; goto Answered; } else if (to.Trim() == "Meters") { convertedValue = input * 0.0254F; goto Answered; } else if (to.Trim() == "Millimeters") { convertedValue = input * 25.4F; goto Answered; } else if (to.Trim() == "Miles") { convertedValue = input * 0.000015783F; goto Answered; } else if (to.Trim() == "Yards") { convertedValue = input * 0.0277778F; goto Answered; } else if (to.Trim() == "Feet") { convertedValue = input * 0.0833333F; goto Answered; } else { convertedValue = input; goto Answered; } } #endregion } else { return ""; } Answered: ShortenUnitsForDisplay abbreviateFrom = new ShortenUnitsForDisplay(from); ShortenUnitsForDisplay abbreviateTo = new ShortenUnitsForDisplay(to); abbreviatedFromUnit = abbreviateFrom.ToString(); abbreviatedToUnit = abbreviateTo.ToString(); string fromAnswerForDisplay, toAnswerForDisplay; sm.RemoveTrailingZeros(input, convertedValue, out fromAnswerForDisplay, out toAnswerForDisplay); answer = System.String.Format("{0} {1}\n =\n{2} {3}", fromAnswerForDisplay, abbreviatedFromUnit, toAnswerForDisplay, abbreviatedToUnit); return answer; }
internal static string ConvertTemp(string from, string to, float input) { string answer; float convertedValue = 0.0F; string abbreviatedFromUnit; string abbreviatedToUnit; SharedMethods sm = new SharedMethods(); if (from != null && to != null) { #region Convert from Fahrenheit if (from.Trim() == "Fahrenheit") { if (to.Trim() == "Celsius") { convertedValue = (input - 32F) * 0.555555F; goto Answered; } else if (to.Trim() == "Kelvin") { convertedValue = (input + 459.67F) * 0.555555F; goto Answered; } else { convertedValue = input; goto Answered; } } #endregion #region Convert from Celsius if (from.Trim() == "Celsius") { if (to.Trim() == "Fahrenheit") { convertedValue = input * 1.8F + 32F; goto Answered; } else if (to.Trim() == "Kelvin") { convertedValue = input + 273.15F; goto Answered; } else { convertedValue = input; goto Answered; } } #endregion #region Convert from Kelvin if (from.Trim() == "Kelvin") { if (to.Trim() == "Fahrenheit") { convertedValue = input * 1.8F - 459.67F; goto Answered; } else if (to.Trim() == "Celsius") { convertedValue = input - 273.15F; goto Answered; } else { convertedValue = input; goto Answered; } } #endregion } else { return ""; } Answered: ShortenUnitsForDisplay abbreviateFrom = new ShortenUnitsForDisplay(from); ShortenUnitsForDisplay abbreviateTo = new ShortenUnitsForDisplay(to); abbreviatedFromUnit = abbreviateFrom.ToString(); abbreviatedToUnit = abbreviateTo.ToString(); string fromAnswerForDisplay, toAnswerForDisplay; sm.RemoveTrailingZeros(input, convertedValue, out fromAnswerForDisplay, out toAnswerForDisplay); answer = System.String.Format("{0} {1}\n =\n{2} {3}", fromAnswerForDisplay, abbreviatedFromUnit, toAnswerForDisplay, abbreviatedToUnit); return answer; }