RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
ConvUtils.Convert Function

Converts a measurement from one set of units to another.

Pascal
function Convert(const AValue: Double; const AFrom: TConvType; const ATo: TConvType): Double; overload;
function Convert(const AValue: Double; const AFrom1: TConvType; const AFrom2: TConvType; const ATo1: TConvType; const ATo2: TConvType): Double; overload;
C++
Double Convert(const Double AValue, const TConvType AFrom, const TConvType ATo);
Double Convert(const Double AValue, const TConvType AFrom1, const TConvType AFrom2, const TConvType ATo1, const TConvType ATo2);

Call Convert to convert the value AValue from one set of measurement units to another. 

Using the first syntax, the AFrom parameter specifies the current units of the measurement AValue. Convert returns the corresponding measurement in the units specified by ATo. For example, the following line converts a temperature measurement from degrees Fahrenheit to degrees Kelvin:

Note: TempInKelvin := Convert(StrToFloat(Edit1.Text), tuFahrenheit, tuKelvin);
Note: TempInKelvin = Convert(StrToFloat(Edit1->Text), tuFahrenheit, tuKelvin);
Using the second syntax, the AFrom1 parameter and AFrom2 parameter specify the current units of AValue, where the units for AValue are AFrom1 per AFrom2. Convert returns the corresponding measurement in the units ATo1 per ATo2. For example, the following call converts miles per gallon to kilometers per liter:
Note: nKPL := Convert(StrToFloat(Edit1.Text), duMiles, vuGallons, duKilometers, vuLiter);
Note: double nKPL = Convert(StrToFloat(Edit1.Text), duMiles, vuGallons, duKilometers, vuLiter);
The units specified by AFrom and ATo, AFrom1 and ATo1, and AFrom2 and ATo2 must be in the same conversion family (they must measure the same thing). You can check whether two TConvType values are in the same conversion family by calling CompatibleConversionTypes. If the units are not compatible, Convert raises an EConversionError exception.
Note: The StdConvs unit defines several families of TConvType values. See conversion family variables for a list of the predefined families of measurement units and the measurement units in each family. In addition, you can register your own conversion families and units using the RegisterConversionType and RegisterConversionFamily functions.
Note: Convert performs its conversion by converting AValue from the AFrom units to the base units of its conversion family, and then from the base units to the ATo units. If the AFrom units and the ATo units differ greatly from the base units of the conversion family, this can lead to problems such as overflow. You can ameliorate these problems by registering your own conversion families that are narrower subsets of a broad conversion family.
 

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!