Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mixing doubles and integers 2

Status
Not open for further replies.

lespaul

Programmer
Feb 4, 2002
7,083
US
I am trying to figure out payments for restitution and a final payment amount and am having problems mixing doubles and integers.

Say a person is ordered to pay $264.53 making a $50 payment. I need to be able to calculate 5 payments of $50 and a final payment of 14.53.

I have a function that takes the total amount due and the payment amount (there are some other parameters, but they aren't neccessary for this part of the function). I want to be able to come up with an integer for the 5 payments, but it's not working correctly. Here's what I've got:

Code:
function PaymentDetailsText(VicName : string; BeginDate : String; PayDay : string; Payment : double; TotalAmt : double; Frequency : integer): string;
var
NumOfPayments : double;
LastPayAmount : double;
begin
  if (Payment > 0) and (TotalAmt > 0) then
  begin
    NumOfPayments := TotalAmt / Payment;
    lastPayAmount := TotalAmt - (Payment * NumOfPayments);
  end
  else
    NumOfPayments := 1;

  result := 'I agree to pay $' + FloatToStr(TotalAmt) + ' to ' + VicName + ', making ' + FloatToSTr(NumOfpayments);
  if NumOfPayments > 1 then
  begin
    result := result + ' payments of $' + FloatToSTr(Payment);
    if lastPayAmount > 0 then
      result := result + ' and a final payment of $' + FloatToSTr(lastPayAmount);
  end
  else
    result := result + ' payment of $' + FloatToSTr(TotalAmt);

  result := result + ' beginning ' + ConvertAS400Date(BeginDate) + ' and';

  Case Frequency of
    1 : result := result + ' monthly on the ' + PayDay + ' thereafter.';
    2 : result := result + ' every two weeks thereafter.';
    3 : result := result + ' every week thereafter.';
  end;

end;

The final result is a sentence inserted into an agreement like:

I agree to pay $264.53 to VictimName, making 5 payments of $50 and a final payment of $14.53 beginning 10/27/2001 and every week thereafter.

Currently, I am getting:

I agree to pay $264.53 to VictimName, making 5.2906 payments of $50 beginning 10/27/2001 and every week thereafter.

can someone help me get this?

thanks!

leslie

 

Instead of
[tt]
...making ' + FloatToSTr(NumOfpayments);
[/tt]
try
[tt]
...making ' + IntToSTr(Trunc(NumOfpayments));
[/tt]

 
There are a few problems with this code - more of the logic variety than the presentation variety. I might look at it later if I have the time and see what they are, but the change suggested above will only take care of formatting issues and not the computational ones.

With the change above:

I agree to pay $243.62 to George, making 4 payments of $50 and a final payment of $1.24344978758018E-14 beginning 02/21/05 and every week thereafter.
 
Okay, other than "result" being undefined and the function result being undefined.

I made the changes to properly process NumOfPayments and also changed the types to "currency" which would probably serve you better. Posting the whole thing becaue it'd be too hard to track the changes down otherwise:

Code:
function PaymentDetailsText(VicName : string;
               BeginDate : String; PayDay : string;
       Payment : currency; TotalAmt : currency; Frequency : integer): string;
  var
    NumOfPayments : integer;
    LastPayAmount : currency;
    aresult: string;
  begin
    if (Payment > 0) and (TotalAmt > 0) then
      begin
        NumOfPayments := trunc(TotalAmt / Payment);
        lastPayAmount := TotalAmt - (Payment * NumOfPayments);
      end
    else
      NumOfPayments := 1;

    aresult := 'I agree to pay $' + FloatToStr(TotalAmt) + ' to ' + VicName +
     ', making ' + IntToSTr(NumOfpayments);
    if NumOfPayments > 1 then
      begin
        aresult := aresult + ' payments of $' + FloatToSTr(Payment);
        if lastPayAmount > 0 then
          aresult := aresult + ' and a final payment of $'
              + FloatToStr(lastPayAmount);
      end
    else
      aresult := aresult + ' payment of $' + FloatToStr(TotalAmt);

    aresult := aresult + ' beginning ' + BeginDate + ' and';

    Case Frequency of
      1 : aresult := aresult + ' monthly on the ' + PayDay + ' thereafter.';
      2 : aresult := aresult + ' every two weeks thereafter.';
      3 : aresult := aresult + ' every week thereafter.';
    end;

    PaymentDetailsText := aresult;
  end;
 
Hi there,

I find your function a bit unreadable. I rewrote it my way :

Code:
type PayFrequency = (PayMonthly, PayTwoWeekly, PayWeekly);

function PaymentDetailsText(VicName : string; BeginDate : string; PayDay : string; Payment : double; TotalAmt : double; Frequency : PayFrequency): string;

var FreqStr         : string;
    FinalPaymentStr : string;
    NumOfPayments   : integer;
    LastPayAmount   : double;

begin
 Result:='';
 FinalPaymentStr:='';
 LastPayAmount:=0;
 case Frequency of
  PayMonthly   : FreqStr:=Format('monthly on the %s thereafter.',[PayDay]);
  PayTwoWeekly : FreqStr:='every two weeks thereafter.';
  PayWeekly    : FreqStr:='every week thereafter.';
 end;
 if (Payment > 0) and (TotalAmt > 0) then
  begin
   NumOfPayments := Trunc(TotalAmt / Payment);
   LastPayAmount := TotalAmt - (Payment * NumOfPayments);
  end
 else
  NumOfPayments := 1;
 if NumOfPayments > 1 then
  begin
   if LastPayAmount > 0 then
    FinalPaymentStr := Format(' and a final payment of $%.2n',[LastPayAmount]);
   Result := Format('I agree to pay $%.2n to %s, making %d payments of $%0.n%s beginning %s and %s',
             [TotalAmt, VicName, NumOfPayments, Payment, FinalPaymentStr, ConvertAS400Date(BeginDate), FreqStr]);
  end
 else
  Result := Format('I agree to pay $%.2n to %s, making %d payment of $%d beginning %s and %s',
            [TotalAmt, VicName, NumOfPayments, Payment, ConvertAS400Date(BeginDate), FreqStr]);
end;

just another point of view :)

Cheers,
Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
small correction to my routine,

last format string must be :
'I agree to pay $%.2n to %s, making %d payment of $%0.n beginning %s and %s'

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
found a small logical bug :

NumOfPayments could be higer than 1 but lower than 2 (1.1..1.9)

so the correct final code should be

Code:
type PayFrequency = (PayMonthly, PayTwoWeekly, PayWeekly);

function PaymentDetailsText(VicName : string; BeginDate : string; PayDay : string; Payment : double; TotalAmt : double; Frequency : PayFrequency): string;

var FreqStr         : string;
    FinalPaymentStr : string;
    NumOfPayments   : integer;
    LastPayAmount   : double;

begin
 Result:='';
 FinalPaymentStr:='';
 LastPayAmount:=0;
 case Frequency of
  PayMonthly   : FreqStr:=Format('monthly on the %s thereafter.',[PayDay]);
  PayTwoWeekly : FreqStr:='every two weeks thereafter.';
  PayWeekly    : FreqStr:='every week thereafter.';
 end;
 if (Payment > 0) and (TotalAmt > 0) then
  begin
   NumOfPayments := Trunc(TotalAmt / Payment);
   LastPayAmount := TotalAmt - (Payment * NumOfPayments);
  end
 else
  NumOfPayments := 1;
 if LastPayAmount > 0 then
  FinalPaymentStr := Format(' and a final payment of $%.2n',[LastPayAmount]);
 if NumOfPayments > 1 then
  Result := Format('I agree to pay $%.2n to %s, making %d payments of $%0.n%s beginning %s and %s',
            [TotalAmt, VicName, NumOfPayments, Payment, FinalPaymentStr, ConvertAS400Date(BeginDate), FreqStr])
 else
  Result := Format('I agree to pay $%.2n to %s, making 1 payment of $%.0n%s beginning %s and %s',
            [TotalAmt, VicName, Payment, FinalPaymentStr, ConvertAS400Date(BeginDate), FreqStr]);
end;

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
wow! I never expected this! Thanks for everything, i'll be working on it this morning.

les
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top