fpspreadsheet: Add financial function PV and PMT to test_formula_func demo.
git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@3285 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
parent
9fb1a96f20
commit
a62bba2b72
@ -52,6 +52,7 @@
|
||||
<RunParams>
|
||||
<local>
|
||||
<FormatVersion Value="1"/>
|
||||
<LaunchingApplication PathPlusParams="\usr\X11R6\bin\xterm -T 'Lazarus Run Output' -e $(LazarusDir)\tools\runwait.sh $(TargetCmdLine)"/>
|
||||
</local>
|
||||
</RunParams>
|
||||
<RequiredPackages Count="1">
|
||||
@ -80,11 +81,6 @@
|
||||
<Debugging>
|
||||
<DebugInfoType Value="dsDwarf2Set"/>
|
||||
</Debugging>
|
||||
<Options>
|
||||
<Win32>
|
||||
<GraphicApplication Value="True"/>
|
||||
</Win32>
|
||||
</Options>
|
||||
</Linking>
|
||||
</CompilerOptions>
|
||||
<Debugging>
|
||||
|
@ -4,165 +4,194 @@
|
||||
<PathDelim Value="\"/>
|
||||
<Version Value="9"/>
|
||||
<BuildModes Active="Debug"/>
|
||||
<Units Count="4">
|
||||
<Units Count="7">
|
||||
<Unit0>
|
||||
<Filename Value="test_formula_func.pas"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
<UnitName Value="test_formula_func"/>
|
||||
<IsVisibleTab Value="True"/>
|
||||
<TopLine Value="33"/>
|
||||
<CursorPos X="14" Y="47"/>
|
||||
<UsageCount Value="20"/>
|
||||
<TopLine Value="295"/>
|
||||
<CursorPos X="19" Y="324"/>
|
||||
<UsageCount Value="27"/>
|
||||
<Bookmarks Count="1">
|
||||
<Item0 X="21" Y="280" ID="1"/>
|
||||
</Bookmarks>
|
||||
<Loaded Value="True"/>
|
||||
</Unit0>
|
||||
<Unit1>
|
||||
<Filename Value="..\..\fpspreadsheet.pas"/>
|
||||
<UnitName Value="fpspreadsheet"/>
|
||||
<EditorIndex Value="3"/>
|
||||
<TopLine Value="2942"/>
|
||||
<CursorPos Y="2961"/>
|
||||
<UsageCount Value="10"/>
|
||||
<EditorIndex Value="4"/>
|
||||
<TopLine Value="1216"/>
|
||||
<CursorPos X="9" Y="1128"/>
|
||||
<UsageCount Value="13"/>
|
||||
<Loaded Value="True"/>
|
||||
</Unit1>
|
||||
<Unit2>
|
||||
<Filename Value="..\..\fpsfunc.pas"/>
|
||||
<UnitName Value="fpsfunc"/>
|
||||
<EditorIndex Value="2"/>
|
||||
<TopLine Value="1594"/>
|
||||
<CursorPos X="35" Y="1603"/>
|
||||
<UsageCount Value="10"/>
|
||||
<EditorIndex Value="3"/>
|
||||
<TopLine Value="113"/>
|
||||
<CursorPos X="10" Y="132"/>
|
||||
<UsageCount Value="13"/>
|
||||
<Loaded Value="True"/>
|
||||
</Unit2>
|
||||
<Unit3>
|
||||
<Filename Value="..\..\fpsutils.pas"/>
|
||||
<UnitName Value="fpsutils"/>
|
||||
<EditorIndex Value="1"/>
|
||||
<EditorIndex Value="2"/>
|
||||
<TopLine Value="876"/>
|
||||
<CursorPos Y="894"/>
|
||||
<UsageCount Value="10"/>
|
||||
<UsageCount Value="13"/>
|
||||
<Loaded Value="True"/>
|
||||
</Unit3>
|
||||
<Unit4>
|
||||
<Filename Value="..\..\fpolebasic.pas"/>
|
||||
<UnitName Value="fpolebasic"/>
|
||||
<EditorIndex Value="1"/>
|
||||
<TopLine Value="43"/>
|
||||
<CursorPos X="29" Y="53"/>
|
||||
<UsageCount Value="13"/>
|
||||
<Loaded Value="True"/>
|
||||
</Unit4>
|
||||
<Unit5>
|
||||
<Filename Value="..\..\xlsbiff8.pas"/>
|
||||
<UnitName Value="xlsbiff8"/>
|
||||
<EditorIndex Value="5"/>
|
||||
<TopLine Value="63"/>
|
||||
<CursorPos X="20" Y="83"/>
|
||||
<UsageCount Value="12"/>
|
||||
<Loaded Value="True"/>
|
||||
</Unit5>
|
||||
<Unit6>
|
||||
<Filename Value="..\..\xlscommon.pas"/>
|
||||
<UnitName Value="xlscommon"/>
|
||||
<EditorIndex Value="6"/>
|
||||
<TopLine Value="1084"/>
|
||||
<CursorPos Y="1103"/>
|
||||
<UsageCount Value="12"/>
|
||||
<Loaded Value="True"/>
|
||||
</Unit6>
|
||||
</Units>
|
||||
<JumpHistory Count="30" HistoryIndex="29">
|
||||
<Position1>
|
||||
<Filename Value="..\..\fpsutils.pas"/>
|
||||
<Caret Line="856" TopLine="836"/>
|
||||
<Filename Value="..\..\fpspreadsheet.pas"/>
|
||||
<Caret Line="1521" TopLine="1501"/>
|
||||
</Position1>
|
||||
<Position2>
|
||||
<Filename Value="..\..\fpsutils.pas"/>
|
||||
<Caret Line="858" TopLine="836"/>
|
||||
<Filename Value="..\..\fpspreadsheet.pas"/>
|
||||
<Caret Line="1522" TopLine="1501"/>
|
||||
</Position2>
|
||||
<Position3>
|
||||
<Filename Value="..\..\fpsutils.pas"/>
|
||||
<Caret Line="860" TopLine="836"/>
|
||||
<Filename Value="..\..\fpspreadsheet.pas"/>
|
||||
<Caret Line="1523" TopLine="1501"/>
|
||||
</Position3>
|
||||
<Position4>
|
||||
<Filename Value="..\..\fpsutils.pas"/>
|
||||
<Caret Line="861" TopLine="836"/>
|
||||
<Filename Value="..\..\fpspreadsheet.pas"/>
|
||||
<Caret Line="1548" TopLine="1544"/>
|
||||
</Position4>
|
||||
<Position5>
|
||||
<Filename Value="..\..\fpsutils.pas"/>
|
||||
<Caret Line="863" TopLine="836"/>
|
||||
<Filename Value="..\..\fpspreadsheet.pas"/>
|
||||
<Caret Line="1562" TopLine="1544"/>
|
||||
</Position5>
|
||||
<Position6>
|
||||
<Filename Value="..\..\fpsutils.pas"/>
|
||||
<Caret Line="864" TopLine="836"/>
|
||||
<Filename Value="..\..\fpspreadsheet.pas"/>
|
||||
<Caret Line="1452" TopLine="1432"/>
|
||||
</Position6>
|
||||
<Position7>
|
||||
<Filename Value="..\..\fpsutils.pas"/>
|
||||
<Caret Line="865" TopLine="836"/>
|
||||
<Filename Value="..\..\fpspreadsheet.pas"/>
|
||||
<Caret Line="1562" TopLine="1543"/>
|
||||
</Position7>
|
||||
<Position8>
|
||||
<Filename Value="..\..\fpsutils.pas"/>
|
||||
<Caret Line="867" TopLine="837"/>
|
||||
<Filename Value="..\..\fpspreadsheet.pas"/>
|
||||
<Caret Line="1567" TopLine="1543"/>
|
||||
</Position8>
|
||||
<Position9>
|
||||
<Filename Value="..\..\fpsutils.pas"/>
|
||||
<Caret Line="875" TopLine="857"/>
|
||||
<Filename Value="..\..\fpspreadsheet.pas"/>
|
||||
<Caret Line="1573" TopLine="1543"/>
|
||||
</Position9>
|
||||
<Position10>
|
||||
<Filename Value="..\..\fpsutils.pas"/>
|
||||
<Caret Line="876" TopLine="857"/>
|
||||
<Filename Value="test_formula_func.pas"/>
|
||||
<Caret Line="125" TopLine="105"/>
|
||||
</Position10>
|
||||
<Position11>
|
||||
<Filename Value="..\..\fpsutils.pas"/>
|
||||
<Caret Line="879" TopLine="857"/>
|
||||
<Filename Value="test_formula_func.pas"/>
|
||||
<Caret Line="126" TopLine="105"/>
|
||||
</Position11>
|
||||
<Position12>
|
||||
<Filename Value="..\..\fpsutils.pas"/>
|
||||
<Caret Line="876" TopLine="857"/>
|
||||
<Filename Value="test_formula_func.pas"/>
|
||||
<Caret Line="128" TopLine="105"/>
|
||||
</Position12>
|
||||
<Position13>
|
||||
<Filename Value="..\..\fpsutils.pas"/>
|
||||
<Caret Line="879" TopLine="857"/>
|
||||
<Filename Value="test_formula_func.pas"/>
|
||||
<Caret Line="133" TopLine="105"/>
|
||||
</Position13>
|
||||
<Position14>
|
||||
<Filename Value="..\..\fpsutils.pas"/>
|
||||
<Caret Line="877" TopLine="857"/>
|
||||
<Filename Value="test_formula_func.pas"/>
|
||||
<Caret Line="130" TopLine="105"/>
|
||||
</Position14>
|
||||
<Position15>
|
||||
<Filename Value="..\..\fpsutils.pas"/>
|
||||
<Caret Line="879" TopLine="857"/>
|
||||
<Filename Value="test_formula_func.pas"/>
|
||||
<Caret Line="133" TopLine="105"/>
|
||||
</Position15>
|
||||
<Position16>
|
||||
<Filename Value="..\..\fpsutils.pas"/>
|
||||
<Caret Line="878" TopLine="857"/>
|
||||
<Filename Value="test_formula_func.pas"/>
|
||||
<Caret Line="131" TopLine="105"/>
|
||||
</Position16>
|
||||
<Position17>
|
||||
<Filename Value="..\..\fpsutils.pas"/>
|
||||
<Caret Line="879" TopLine="857"/>
|
||||
<Filename Value="test_formula_func.pas"/>
|
||||
<Caret Line="133" TopLine="105"/>
|
||||
</Position17>
|
||||
<Position18>
|
||||
<Filename Value="..\..\fpsutils.pas"/>
|
||||
<Caret Line="878" TopLine="857"/>
|
||||
<Filename Value="test_formula_func.pas"/>
|
||||
<Caret Line="132" TopLine="105"/>
|
||||
</Position18>
|
||||
<Position19>
|
||||
<Filename Value="..\..\fpsutils.pas"/>
|
||||
<Caret Line="879" TopLine="857"/>
|
||||
<Filename Value="test_formula_func.pas"/>
|
||||
<Caret Line="133" TopLine="105"/>
|
||||
</Position19>
|
||||
<Position20>
|
||||
<Filename Value="..\..\fpsutils.pas"/>
|
||||
<Caret Line="876" TopLine="857"/>
|
||||
<Filename Value="test_formula_func.pas"/>
|
||||
<Caret Line="129" TopLine="105"/>
|
||||
</Position20>
|
||||
<Position21>
|
||||
<Filename Value="..\..\fpsutils.pas"/>
|
||||
<Caret Line="879" TopLine="857"/>
|
||||
<Filename Value="test_formula_func.pas"/>
|
||||
<Caret Line="133" TopLine="105"/>
|
||||
</Position21>
|
||||
<Position22>
|
||||
<Filename Value="..\..\fpsutils.pas"/>
|
||||
<Caret Line="894" TopLine="876"/>
|
||||
<Filename Value="test_formula_func.pas"/>
|
||||
<Caret Line="90" TopLine="70"/>
|
||||
</Position22>
|
||||
<Position23>
|
||||
<Filename Value="..\..\fpspreadsheet.pas"/>
|
||||
<Caret Line="2997" TopLine="2966"/>
|
||||
<Filename Value="test_formula_func.pas"/>
|
||||
<Caret Line="75" Column="25" TopLine="71"/>
|
||||
</Position23>
|
||||
<Position24>
|
||||
<Filename Value="..\..\fpspreadsheet.pas"/>
|
||||
<Caret Line="2998" TopLine="2967"/>
|
||||
<Filename Value="test_formula_func.pas"/>
|
||||
<Caret Line="127" Column="34" TopLine="102"/>
|
||||
</Position24>
|
||||
<Position25>
|
||||
<Filename Value="..\..\fpspreadsheet.pas"/>
|
||||
<Caret Line="2961" TopLine="2942"/>
|
||||
<Filename Value="test_formula_func.pas"/>
|
||||
<Caret Line="280" TopLine="261"/>
|
||||
</Position25>
|
||||
<Position26>
|
||||
<Filename Value="..\..\fpsfunc.pas"/>
|
||||
<Caret Line="50" TopLine="24"/>
|
||||
<Filename Value="..\..\fpspreadsheet.pas"/>
|
||||
<Caret Line="1543" Column="32" TopLine="1543"/>
|
||||
</Position26>
|
||||
<Position27>
|
||||
<Filename Value="..\..\fpsfunc.pas"/>
|
||||
<Caret Line="1607" Column="52" TopLine="1591"/>
|
||||
<Filename Value="..\..\fpspreadsheet.pas"/>
|
||||
</Position27>
|
||||
<Position28>
|
||||
<Filename Value="..\..\fpsfunc.pas"/>
|
||||
<Caret Line="1606" Column="16" TopLine="1592"/>
|
||||
<Filename Value="..\..\fpspreadsheet.pas"/>
|
||||
<Caret Line="67" Column="34" TopLine="35"/>
|
||||
</Position28>
|
||||
<Position29>
|
||||
<Filename Value="..\..\fpsfunc.pas"/>
|
||||
<Caret Line="1610" Column="8" TopLine="1593"/>
|
||||
<Filename Value="..\..\fpspreadsheet.pas"/>
|
||||
<Caret Line="1121" Column="10" TopLine="1089"/>
|
||||
</Position29>
|
||||
<Position30>
|
||||
<Filename Value="test_formula_func.pas"/>
|
||||
<Caret Line="61" Column="24" TopLine="39"/>
|
||||
<Caret Line="269" Column="29" TopLine="261"/>
|
||||
</Position30>
|
||||
</JumpHistory>
|
||||
</ProjectSession>
|
||||
|
@ -1,18 +1,34 @@
|
||||
{ This demo show how a user-provided function can be used for calculation of
|
||||
{ This demo show how user-provided functions can be used for calculation of
|
||||
rpn formulas that are built-in to fpspreadsheet, but don't have an own
|
||||
calculation procedure. }
|
||||
calculation procedure.
|
||||
|
||||
The example will show implementation of the some financial formulas:
|
||||
- FV(...) (future value)
|
||||
- PV(...) (present value)
|
||||
- PMT(...) (payment)
|
||||
|
||||
The demo writes an xls file which uses these formulas and then displays
|
||||
the result in a console window. (Open the generated file in Excel or
|
||||
Open/LibreOffice and compare).
|
||||
}
|
||||
|
||||
program test_formula_func;
|
||||
|
||||
{$mode objfpc}{$H+}
|
||||
//{$mode objfpc}{$H+}
|
||||
{$mode delphi}{$H+}
|
||||
|
||||
uses
|
||||
{$IFDEF UNIX}{$IFDEF UseCThreads}
|
||||
{$IFDEF UNIX}{$IFDEF UseCThreads}
|
||||
cthreads,
|
||||
{$ENDIF}{$ENDIF}
|
||||
Classes, laz_fpspreadsheet
|
||||
Classes, SysUtils, laz_fpspreadsheet
|
||||
{ you can add units after this },
|
||||
math, fpspreadsheet, fpsfunc;
|
||||
math, fpspreadsheet, xlsbiff8, fpsfunc;
|
||||
|
||||
|
||||
{------------------------------------------------------------------------------}
|
||||
{ Basic implmentation of the three financial funtions }
|
||||
{------------------------------------------------------------------------------}
|
||||
|
||||
const
|
||||
paymentAtEnd = 0;
|
||||
@ -24,24 +40,72 @@ const
|
||||
- "number_periods" is the number of payment periods, i.e. number of payments
|
||||
for the annuity.
|
||||
- "payment" is the amount of the payment made each period
|
||||
- "PV" is the present value of the payments.
|
||||
- "pv" is the present value of the payments.
|
||||
- "payment_type" indicates when the payments are due (see paymentAtXXX constants)
|
||||
see: http://en.wikipedia.org/wiki/Future_value
|
||||
see: http://en.wikipedia.org/wiki/Time_value_of_money
|
||||
|
||||
In Excel's implementation the payments and the FV add up to 0:
|
||||
FV + PV q^n + PMT (q^n - 1) / (q - 1) = 0
|
||||
}
|
||||
function FV(interest_rate, number_periods, payment, pv: Double;
|
||||
function FV(interest_rate: Double; number_periods: Integer; payment, pv: Double;
|
||||
payment_type: integer): Double;
|
||||
var
|
||||
q: Double;
|
||||
q, qn, factor: Double;
|
||||
begin
|
||||
q := 1.0 + interest_rate;
|
||||
|
||||
Result := pv * power(q, number_periods) +
|
||||
(power(q, number_periods) - 1) / (q - 1) * payment;
|
||||
|
||||
qn := power(q, number_periods);
|
||||
factor := (qn - 1) / (q - 1);
|
||||
if payment_type = paymentAtBegin then
|
||||
Result := Result * q;
|
||||
factor := factor * q;
|
||||
|
||||
Result := -(pv * qn + payment*factor);
|
||||
end;
|
||||
|
||||
{ Calculates the regular payments for a loan based on an interest rate and a
|
||||
constant payment schedule
|
||||
Arguments as shown for FV(), in addition:
|
||||
- "fv" is the future value of the payments.
|
||||
see: http://en.wikipedia.org/wiki/Time_value_of_money
|
||||
}
|
||||
function PMT(interest_rate: Double; number_periods: Integer; pv, fv: Double;
|
||||
payment_type: Integer): Double;
|
||||
var
|
||||
q, qn, factor: Double;
|
||||
begin
|
||||
q := 1.0 + interest_rate;
|
||||
qn := power(q, number_periods);
|
||||
factor := (qn - 1) / (q - 1);
|
||||
if payment_type = paymentAtBegin then
|
||||
factor := factor * q;
|
||||
|
||||
Result := -(fv + pv * qn) / factor;
|
||||
end;
|
||||
|
||||
{ Calculates the present value of an investment based on an interest rate and
|
||||
a constant payment schedule.
|
||||
Arguments as shown for FV(), in addition:
|
||||
- "fv" is the future value of the payments.
|
||||
see: http://en.wikipedia.org/wiki/Time_value_of_money
|
||||
}
|
||||
function PV(interest_rate: Double; number_periods: Integer; payment, fv: Double;
|
||||
payment_type: Integer): Double;
|
||||
var
|
||||
q, qn, factor: Double;
|
||||
begin
|
||||
q := 1.0 + interest_rate;
|
||||
qn := power(q, number_periods);
|
||||
factor := (qn - 1) / (q - 1);
|
||||
if payment_type = paymentAtBegin then
|
||||
factor := factor * q;
|
||||
|
||||
Result := -(fv + payment*factor) / qn;
|
||||
end;
|
||||
|
||||
|
||||
{------------------------------------------------------------------------------}
|
||||
{ Adaption for usage by fpspreadsheet }
|
||||
{------------------------------------------------------------------------------}
|
||||
|
||||
function fpsFV(Args: TsArgumentStack; NumArgs: Integer): TsArgument;
|
||||
var
|
||||
data: TsArgNumberArray;
|
||||
@ -52,14 +116,47 @@ begin
|
||||
if Args.PopNumberValues(NumArgs, false, data, Result) then
|
||||
// Call our FV function with the NumberValues of the arguments.
|
||||
Result := CreateNumberArg(FV(
|
||||
data[0], // interest rate
|
||||
data[1], // number of payments
|
||||
data[2], // payment
|
||||
data[3], // present value
|
||||
round(data[4]) // payment type
|
||||
data[0], // interest rate
|
||||
round(data[1]), // number of payments
|
||||
data[2], // payment
|
||||
data[3], // present value
|
||||
round(data[4]) // payment type
|
||||
));
|
||||
end;
|
||||
|
||||
function fpsPMT(Args: TsArgumentStack; NumArgs: Integer): TsArgument;
|
||||
var
|
||||
data: TsArgNumberArray;
|
||||
begin
|
||||
if Args.PopNumberValues(NumArgs, false, data, Result) then
|
||||
Result := CreateNumberArg(PMT(
|
||||
data[0], // interest rate
|
||||
round(data[1]), // number of payments
|
||||
data[2], // present value
|
||||
data[3], // future value
|
||||
round(data[4]) // payment type
|
||||
));
|
||||
end;
|
||||
|
||||
function fpsPV(Args: TsArgumentStack; NumArgs: Integer): TsArgument;
|
||||
var
|
||||
data: TsArgNumberArray;
|
||||
begin
|
||||
if Args.PopNumberValues(NumArgs, false, data, Result) then
|
||||
Result := CreateNumberArg(PV(
|
||||
data[0], // interest rate
|
||||
round(data[1]), // number of payments
|
||||
data[2], // payment
|
||||
data[3], // future value
|
||||
round(data[4]) // payment type
|
||||
));
|
||||
end;
|
||||
|
||||
|
||||
{------------------------------------------------------------------------------}
|
||||
{ Write xls file comparing our own calculations with Excel result }
|
||||
{------------------------------------------------------------------------------}
|
||||
procedure WriteFile(AFileName: String);
|
||||
const
|
||||
INTEREST_RATE = 0.03;
|
||||
NUMBER_PAYMENTS = 10;
|
||||
@ -70,54 +167,171 @@ const
|
||||
var
|
||||
workbook: TsWorkbook;
|
||||
worksheet: TsWorksheet;
|
||||
fval, pval, pmtval: Double;
|
||||
|
||||
begin
|
||||
{ We have to register our financial function in fpspreadsheet. Otherwise an
|
||||
error code would be displayed in the reading part of this demo in these
|
||||
formula cells. }
|
||||
RegisterFormulaFunc(fekFV, @fpsFV);
|
||||
RegisterFormulaFunc(fekPMT, @fpsPMT);
|
||||
RegisterFormulaFunc(fekPV, @fpsPV);
|
||||
|
||||
workbook := TsWorkbook.Create;
|
||||
try
|
||||
worksheet := workbook.AddWorksheet('Financial');
|
||||
worksheet.Options := worksheet.Options + [soCalcBeforeSaving];
|
||||
worksheet.WriteColWidth(0, 20);
|
||||
worksheet.WriteColWidth(0, 40);
|
||||
|
||||
worksheet.WriteUTF8Text(0, 0, 'Interest rate');
|
||||
worksheet.WriteNumber(0, 1, INTEREST_RATE, nfPercentage, 1);
|
||||
worksheet.WriteNumber(0, 1, INTEREST_RATE, nfPercentage, 1); // B1
|
||||
|
||||
worksheet.WriteUTF8Text(1, 0, 'Number of payments');
|
||||
worksheet.WriteNumber(1, 1, NUMBER_PAYMENTS);
|
||||
worksheet.WriteNumber(1, 1, NUMBER_PAYMENTS); // B2
|
||||
|
||||
worksheet.WriteUTF8Text(2, 0, 'Payment');
|
||||
worksheet.WriteCurrency(2, 1, PAYMENT, nfCurrency, 2, '$');
|
||||
worksheet.WriteCurrency(2, 1, PAYMENT, nfCurrency, 2, '$'); // B3
|
||||
|
||||
worksheet.WriteUTF8Text(3, 0, 'Present value');
|
||||
worksheet.WriteCurrency(3, 1, PRESENT_VALUE, nfCurrency, 2, '$');
|
||||
worksheet.WriteCurrency(3, 1, PRESENT_VALUE, nfCurrency, 2, '$'); // B4
|
||||
|
||||
worksheet.WriteUTF8Text(4, 0, 'Payment at end');
|
||||
worksheet.WriteBoolValue(4, 1, PAYMENT_WHEN = paymentAtEnd);
|
||||
worksheet.WriteUTF8Text(4, 0, 'Payment at end (0) or at begin (1)');
|
||||
worksheet.WriteNumber(4, 1, PAYMENT_WHEN); // B5
|
||||
|
||||
// future value calculation
|
||||
fval := FV(INTEREST_RATE, NUMBER_PAYMENTS, PAYMENT, PRESENT_VALUE, PAYMENT_WHEN);
|
||||
worksheet.WriteUTF8Text(6, 0, 'Future value');
|
||||
worksheet.WriteFontStyle(6, 0, [fssBold]);
|
||||
worksheet.WriteUTF8Text(7, 0, 'Our calculation');
|
||||
worksheet.WriteCurrency(7, 1,
|
||||
FV(INTEREST_RATE, NUMBER_PAYMENTS, PAYMENT, PRESENT_VALUE, PAYMENT_WHEN),
|
||||
nfCurrency, 2, '$'
|
||||
);
|
||||
worksheet.WriteCurrency(7, 1, fval, nfCurrency, 2, '$');
|
||||
|
||||
worksheet.WriteUTF8Text(8, 0, 'Excel''s calculation');
|
||||
worksheet.WriteUTF8Text(8, 0, 'Excel''s calculation using constants');
|
||||
worksheet.WriteNumberFormat(8, 1, nfCurrency, 2, '$');
|
||||
worksheet.WriteRPNFormula(8, 1, CreateRPNFormula(
|
||||
worksheet.WriteRPNFormula(8, 1, CreateRPNFormula( // B9
|
||||
RPNNumber(INTEREST_RATE,
|
||||
RPNNumber(NUMBER_PAYMENTS,
|
||||
RPNNumber(-PAYMENT,
|
||||
RPNNumber(-PRESENT_VALUE,
|
||||
RPNNumber(PAYMENT,
|
||||
RPNNumber(PRESENT_VALUE,
|
||||
RPNNumber(PAYMENT_WHEN,
|
||||
RPNFunc(fekFV, 5,
|
||||
nil))))))));
|
||||
worksheet.WriteUTF8Text(9, 0, 'Excel''s calculation using cell values');
|
||||
worksheet.WriteNumberFormat(9, 1, nfCurrency, 2, '$');
|
||||
worksheet.WriteRPNFormula(9, 1, CreateRPNFormula( // B9
|
||||
RPNCellValue('B1', // interest rate
|
||||
RPNCellValue('B2', // number of periods
|
||||
RPNCellValue('B3', // payment
|
||||
RPNCellValue('B4', // present value
|
||||
RPNCellValue('B5', // payment at end or at start
|
||||
RPNFunc(fekFV, 5, // Call Excel's FV formula
|
||||
nil))))))));
|
||||
|
||||
workbook.WriteToFile('test_fv.xls', sfExcel8, true);
|
||||
// present value calculation
|
||||
pval := PV(INTEREST_RATE, NUMBER_PAYMENTS, PAYMENT, fval, PAYMENT_WHEN);
|
||||
worksheet.WriteUTF8Text(11, 0, 'Present value');
|
||||
worksheet.WriteFontStyle(11, 0, [fssBold]);
|
||||
worksheet.WriteUTF8Text(12, 0, 'Our calculation');
|
||||
worksheet.WriteCurrency(12, 1, pval, nfCurrency, 2, '$');
|
||||
|
||||
worksheet.WriteUTF8Text(13, 0, 'Excel''s calculation using constants');
|
||||
worksheet.WriteNumberFormat(13, 1, nfCurrency, 2, '$');
|
||||
worksheet.WriteRPNFormula(13, 1, CreateRPNFormula(
|
||||
RPNNumber(INTEREST_RATE,
|
||||
RPNNumber(NUMBER_PAYMENTS,
|
||||
RPNNumber(PAYMENT,
|
||||
RPNNumber(fval,
|
||||
RPNNumber(PAYMENT_WHEN,
|
||||
RPNFunc(fekPV, 5,
|
||||
nil))))))));
|
||||
Worksheet.WriteUTF8Text(14, 0, 'Excel''s calculation using cell values');
|
||||
worksheet.WriteNumberFormat(14, 1, nfCurrency, 2, '$');
|
||||
worksheet.WriteRPNFormula(14, 1, CreateRPNFormula(
|
||||
RPNCellValue('B1', // interest rate
|
||||
RPNCellValue('B2', // number of periods
|
||||
RPNCellValue('B3', // payment
|
||||
RPNCellValue('B10', // future value
|
||||
RPNCellValue('B5', // payment at end or at start
|
||||
RPNFunc(fekPV, 5, // Call Excel's PV formula
|
||||
nil))))))));
|
||||
|
||||
// payments calculation
|
||||
pmtval := PMT(INTEREST_RATE, NUMBER_PAYMENTS, PRESENT_VALUE, fval, PAYMENT_WHEN);
|
||||
worksheet.WriteUTF8Text(16, 0, 'Payment');
|
||||
worksheet.WriteFontStyle(16, 0, [fssBold]);
|
||||
worksheet.WriteUTF8Text(17, 0, 'Our calculation');
|
||||
worksheet.WriteCurrency(17, 1, pmtval, nfCurrency, 2, '$');
|
||||
|
||||
worksheet.WriteUTF8Text(18, 0, 'Excel''s calculation using constants');
|
||||
worksheet.WriteNumberFormat(18, 1, nfCurrency, 2, '$');
|
||||
worksheet.WriteRPNFormula(18, 1, CreateRPNFormula(
|
||||
RPNNumber(INTEREST_RATE,
|
||||
RPNNumber(NUMBER_PAYMENTS,
|
||||
RPNNumber(PRESENT_VALUE,
|
||||
RPNNumber(fval,
|
||||
RPNNumber(PAYMENT_WHEN,
|
||||
RPNFunc(fekPMT, 5,
|
||||
nil))))))));
|
||||
Worksheet.WriteUTF8Text(19, 0, 'Excel''s calculation using cell values');
|
||||
worksheet.WriteNumberFormat(19, 1, nfCurrency, 2, '$');
|
||||
worksheet.WriteRPNFormula(19, 1, CreateRPNFormula(
|
||||
RPNCellValue('B1', // interest rate
|
||||
RPNCellValue('B2', // number of periods
|
||||
RPNCellValue('B4', // present value
|
||||
RPNCellValue('B10', // future value
|
||||
RPNCellValue('B5', // payment at end or at start
|
||||
RPNFunc(fekPMT, 5, // Call Excel's PMT formula
|
||||
nil))))))));
|
||||
|
||||
workbook.WriteToFile(AFileName, sfExcel8, true);
|
||||
|
||||
finally
|
||||
workbook.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
{------------------------------------------------------------------------------}
|
||||
{ Read xls file to display Excel's results }
|
||||
{------------------------------------------------------------------------------}
|
||||
procedure ReadFile(AFileName: String);
|
||||
var
|
||||
workbook: TsWorkbook;
|
||||
worksheet: TsWorksheet;
|
||||
r: Cardinal;
|
||||
s1, s2: String;
|
||||
begin
|
||||
workbook := TsWorkbook.Create;
|
||||
try
|
||||
workbook.ReadFormulas := true;
|
||||
workbook.ReadFromFile(AFilename, sfExcel8);
|
||||
|
||||
worksheet := workbook.GetFirstWorksheet;
|
||||
|
||||
// Write all cells with contents to the console
|
||||
WriteLn('');
|
||||
WriteLn('Contents of the first worksheet of the file:');
|
||||
WriteLn('');
|
||||
|
||||
for r := 0 to worksheet.GetLastRowIndex do begin
|
||||
s1 := UTF8ToAnsi(worksheet.ReadAsUTF8Text(r, 0));
|
||||
s2 := UTF8ToAnsi(worksheet.ReadAsUTF8Text(r, 1));
|
||||
if s1 = '' then
|
||||
WriteLn
|
||||
else
|
||||
WriteLn(s1+': ':50, s2);
|
||||
end;
|
||||
|
||||
WriteLn;
|
||||
WriteLn('Press [ENTER] to close...');
|
||||
ReadLn;
|
||||
finally
|
||||
workbook.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
|
||||
begin
|
||||
WriteFile('test_fv.xls');
|
||||
ReadFile('test_fv.xls');
|
||||
end.
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user