TMassUnit = (Undefined, Gram, Kilogram, Milligram, Tonne);
TMass =
record
private
FValue: Extended;
// Wert
FUnit: TMassUnit;
// Einheit
public
constructor Create(AValue: Extended; AUnit: TMassUnit);
property Value: Extended
read FValue;
property &
Unit: TMassUnit
read FUnit;
private
function AsBaseUnit(): Extended;
function AsBaseNumericType(AUnit: TMassUnit);
public
property Grams: Extended
index TMassUnit.Gram
read AsBaseNumericType;
property Kilograms: Extended
index TMassUnit.Kilogram
read AsBaseNumericType;
property Milligrams: Extended
index TMassUnit.Milligram
read AsBaseNumericType;
property Tonnes: Extended
index TMassUnit.Tonne
read AsBaseNumericType;
end;
function TMass.AsBaseUnit(): Extended;
var
x:Extended;
begin
x := Value;
case FUnit
of
TMass.Gram: Result := x / 1e3;
TMass.Kilogram: Result := x;
TMass.Milligram: Result := x / 1e6;
TMass.Tonne: Result := x * 1e3;
else
raise ENotImplementedException.Create('
Fehler');
end;
end;
function TMass.AsBaseNumericType(AUnit: TMassUnit): Extended;
var
x: Extended;
begin
if (AUnit = FUnit)
Exit(FValue);
x := AsBaseUnit();
case AUnit
of
TMass.Gram: Result := x * 1e3;
TMass.Kilogram: Result := x;
TMass.Milligram: Result := x * 1e6;
TMass.Tonne: Result := x / 1e3;
else
raise ENotImplementedException.Create('
Fehler');
end;
end;