Archive

Archive for March, 2007

Built-in Conversion Utils

March 6, 2007 2 comments

Topik kali ini adalah mengenai berbagai macam fungsi konversi yang telah disediakan oleh Delphi namun sering kali tidak kita ketahui karena keterbatasan dalam menggali source code dan Help yang disediakan oleh Delphi.

Sering kali kita membuat fungsi sendiri bagaimana melakukan konversi / perhitungan temperatur udara dari satuan Celcius ke Fahrenheit, atau bagaimana perhitungan konversi jarak dari satuan Kilometer ke satuan Mil, atau bagaimana perhitungan konversi berat dari satuan Kilogram ke satuan Pons dan sebagainya.

Namun, tahukah Anda bahwa sesungguhnya Delphi sudah menyediakan beberapa fungsi konversi yang siap kita gunakan? Ah, mungkin Anda terkejut karena tidak menyangka. Ya Delphi sudah menyediakan fungsi konversi yang siap kita gunakan, mulai dari konversi Luas Bidang, Jarak, Berat, Temperatur, Waktu, Volume bahkan Mata Uang EURO.

Tidak percaya? Coba buka dokumentasi Delphi dan berikan keyword Convert. Dan berikut hasilnya:

Convert Routine

Converts a measurement from one set of units to another.

Unit
ConvUtils

Syntax
[Delphi] function Convert(const AValue: Double; const AFrom: TConvType; const ATo: TConvType): Double; overload;

[Delphi] function Convert(const AValue: Double; const AFrom1: TConvType; const AFrom2: TConvType; const ATo1: TConvType; const ATo2: TConvType): Double; overload;

Description
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.

Jika Anda kurang puas dengan contoh penggunaannya, ada contoh penggunaan fungsi Convert pada folder ConvertIt. Dan sekiranya Anda kurang puas dengan konversi yang disediakan, Anda dapat membuat sendiri rutin konversi Anda. Menarik bukan? 🙂

Categories: Delphi, Did You Know