Algorithm Math Delphi

Title: Odometer Calculations
Question: Finding the change in an odometer value requires special handling for the case of meter rollover. Rollover occurs when the value increases past the maximum that can be represented with a fixed number of digits. This algorithm calculates meter changes with or without rollover and it is independent of the number of digits.
Answer:
Detect the number of digits of the meter by counting the digits in the starting value. The assumption here is that the change in meter value is always less than 90% of the maximum meter value. Once the number of digits of the meter is known, the "missing value" from the rollover can be added to get the correct result.
Here is the code for a button that does a calculation of meter change. The associated form accepts a starting value for the meter and an ending value for the meter. The button is clicked to find the meter change value:
procedure TForm1.Button1Click(Sender: TObject);
var
startVal, endVal, changeVal: integer;
meterDigits, meterMax, i: integer;
begin
startVal := strToInt(edit1.text);
endVal := strToInt(edit2.text);
if (endVal = startVal) then // Meter did not rollover
changeVal := endVal - startVal // Usual calculation
else // Meter rolled over so do special calculation
begin
meterDigits := length(edit1.Text);
meterMax := 1;// Initialize then calculate meter limit
for i := 1 to meterDigits do // Calculate rollover value of meter
meterMax := meterMax * 10; // 10 raised to power of number of digits
changeVal := meterMax + endVal - startVal; // Add the missing value
end; // then subtract
label1.caption := intToStr(changeVal); // Show answer
end;
You can download a working application that demonstrates this at http://briefcase.yahoo.com/wytcom.