Show: Delphi C++
Display Preferences

Performing Conversions

From RAD Studio
Jump to: navigation, search

Go Up to Converting Measurements


You can use the Convert function to perform both simple and complex conversions. It includes a simple syntax and a second syntax for performing conversions between complex measurement types.

Performing simple conversions

You can use the Convert function to convert a measurement from one set of units to another. The Convert function converts between units that measure the same type of thing (distance, area, time, temperature, and so on).

To use Convert, you must specify the units from which to convert and to which to convert. You use the TConvTypetype to identify the units of measurement.

For example, this converts a temperature from degrees Fahrenheit to degrees Kelvin:

Delphi:

 TempInKelvin := Convert(StrToFloat(Edit1.Text), tuFahrenheit, tuKelvin);

C++:

TempInKelvin = Convert(StrToFloat(Edit1->Text), tuFahrenheit, tuKelvin);

Performing complex conversions

You can also use the Convert function to perform more complex conversions between the ratio of two measurement types. Examples of when you might need to use this this are when converting miles per hour to meters per minute for calculating speed or when converting gallons per minute to liters per hour for calculating flow.

For example, the following call converts miles per gallon to kilometers per liter:

Delphi:

 nKPL := Convert(StrToFloat(Edit1.Text), duMiles, vuGallons, duKilometers, vuLiter);

C++:

double nKPL = Convert(StrToFloat(Edit1.Text), duMiles, vuGallons, duKilometers, vuLiter);

The units you are converting must be in the same conversion family (they must measure the same thing). If the units are not compatible, Convert raises an EConversionError exception. You can check whether two TConvType values are in the same conversion family by calling CompatibleConversionTypes.

The System.StdConvs unit defines several families of TConvType values.

See Also

Personal tools
RAD Studio 10.2 Tokyo
In other languages
Previous Versions
Assistance