System.ConvUtils.Convert

From RAD Studio API Documentation
Jump to: navigation, search

Delphi

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

C++

extern DELPHI_PACKAGE double __fastcall Convert(const double AValue, const TConvType AFrom, const TConvType ATo)/* overload */;

Properties

Type Visibility Source Unit Parent
function public
System.ConvUtils.pas
System.ConvUtils.hpp
System.ConvUtils System.ConvUtils

Description

Converts a measurement from one set of units to another.

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

Using the first syntax, the AFrom parameter specifies the current units of the AValue measurement. 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:

TempInKelvin := Convert(StrToFloat(Edit1.Text), tuFahrenheit, tuKelvin);
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:

nKPL := Convert(StrToFloat(Edit1.Text), duMiles, vuGallons, duKilometers, vuLiter);
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.

See Also