1.0.2 • Published 5 months ago

calculate-tool v1.0.2

Weekly downloads
-
License
ISC
Repository
github
Last release
5 months ago

calculate-tool

A commonly used calculator written by Typescript, including calculations related to finance, statistics, etc.

中文

node:

$ npm install calculate-tool --save
# or
$ yarn add calculate-tool
# or
$ pnpm add calculate-tool
# or
$ bun add calculate-tool

Usage

ES6

import { fvInDfc } from 'calculate-tool';
fvInDfc({
	n: 20,
	pv: 0,
	pmt: 1000,
	rate: 8
});
// return: -45761.9643

CMD

const {fvInDfc} = require('calculate-tool');
fvInDfc({
	n: 20,
	pv: 0,
	pmt: 1000,
	rate: 8
});

Globals

calculatorTool.fvInDfc({
	n: 20,
	pv: 0,
	pmt: 1000,
	rate: 8
});

Features

  1. methods
  1. Includes a TypeScript declaration file: lib/index.d.ts

API

fvInDfc(Parameters)

Get the future value of annuity in discounted cash flow model

Examples

import { fvInDfc } from 'calculate-tool';
fvInDfc({
	n: 20,
	pv: 0,
	pmt: 1000,
	rate: 8
});
// return: -45761.9643

Parameters

NameDescriptionTypeAttributesDefault
nTotal Number of payment periodsNumber
pvThe present valueNumber
pmtPayment per periodNumber
rateThe interest rate per period;If the real interest rate is 8%, the return is 8Number
decimalReserved decimal places for resultsNumber\<optional>4
isEndPrepaid Annuity or Ordinary Annuity;If true, it's Ordinary AnnuityBoolean\<optional>true

Returns

Type: Number

pvInDfc(Parameters)

Get the present value of annuity in discounted cash flow model

Examples

import { pvInDfc } from 'calculate-tool';
pvInDfc({
	n: 20,
	fv: 50000,
	pmt: 1000,
	rate: 8
});
// return: -15465.56

Parameters

NameDescriptionTypeAttributesDefault
nTotal Number of payment periodsNumber
fvThe future valueNumber
pmtPayment per periodNumber
rateThe interest rate per period;If the real interest rate is 8%, the return is 8Number
decimalReserved decimal places for resultsNumber\<optional>4
isEndPrepaid Annuity or Ordinary Annuity;If true, it's Ordinary AnnuityBoolean\<optional>true

Returns

Type: Number

pmtInDfc(Parameters)

Get the payment of annuity in discounted cash flow model

Examples

import { pmtInDfc } from 'calculate-tool';
pmtInDfc({
	n: 20,
	fv: 50000,
	pv:  -15465.56
	rate: 8
});
// return: 1000

Parameters

NameDescriptionTypeAttributesDefault
nTotal Number of payment periodsNumber
fvThe future valueNumber
pvThe present valueNumber
rateThe interest rate per period;If the real interest rate is 8%, the return is 8Number
decimalReserved decimal places for resultsNumber\<optional>4
isEndPrepaid Annuity or Ordinary Annuity;If true, it's Ordinary AnnuityBoolean\<optional>true

Returns

Type: Number

rateInDfc(Parameters)

Get the interest rate per period in discounted cash flow model.If the return is 8, the real interest rate is 8%.

Examples

import { rateInDfc } from 'calculate-tool';
rateInDfc({
	n: 20,
	fv: 50000,
	pv: -15465.56,
	pmt: 1000
});
// return: 8

Parameters

NameDescriptionTypeAttributesDefault
nTotal Number of payment periodsNumber
fvThe future valueNumber
pvThe present valueNumber
pmtPayment per periodNumber
decimalReserved decimal places for resultsNumber\<optional>4
isEndPrepaid Annuity or Ordinary Annuity;If true, it's Ordinary AnnuityBoolean\<optional>true

Returns

Type: Number

npv(Parameters)

Get the Net Present Value in discounted cash flow model.

Examples

import { npv } from 'calculate-tool';
npv({
	initCf: -1000,
	cfList: [500, 500, 500],
	rate: 336.506
});
// return: 8

Parameters

NameDescriptionTypeAttributesDefault
initCfThe initial cash flowNumber
cfListCash flow listArray\<Number>
rateThe interest rate per periodNumber
decimalReserved decimal places for resultsNumber\<optional>4

Returns

Type: Number

irr(Parameters)

Get the Internal Rate of Return in discounted cash flow model.

Examples

import { irr } from 'calculate-tool';
irr({
	initCf: -5000,
	cfList: [1000, 3000, 1500]
});
// return: 4.687

Parameters

NameDescriptionTypeAttributesDefault
initCfThe initial cash flowNumber
cfListCash flow listArray\<Number>
decimalReserved decimal places for resultsNumber\<optional>4

Returns

Type: Number

pmtInloan(Parameters)

Get the payment per period of the loan.

Examples

If you secure a loan of \$3,000,000 with a 30-year term, a monthly repayment schedule, an annual interest rate of 5%, and a repayment method of equal repayment, you will be required to make a monthly repayment of \$16,104.65.

import { pmtInloan } from 'calculate-tool';
pmtInloan({
	principal: 3000000,
	rate: 5/12,
	periods: 30 * 12,
	isEqualPayment: true,
	decimal: 2
})
// return: 16104.65

Parameters

NameDescriptionTypeAttributesDefault
principalThe Principle of the loanNumber
rateThe interst rate of every period;Number
periodsNumber of loan termsNumber
currentPeriodThe current period of the loan, If isEqualPayment is false, the currentPeriod is requiredNumber\<optional>
isEqualPaymentThe repayment method;If true, it's Fixed Repayment Loan; If false, it's Equal Principal Payment LoanBoolean\<optional>true
decimalReserved decimal places for resultsNumber\<optional>4

notes

Fixed Repayment Loan: The repayment amount is the same for each term Equal Principal Payment Loan: The principal is the same for each term

Returns

Type: Number

repaymentScheduleInLoan(Parameters)

Get the repayment schedule in the loan

Examples

If you secure a loan of $30,000 with a 4-month term, a monthly repayment schedule, an annual interest rate of 5%, and a repayment method of equal repayment, the following is your repayment plan list

import { repaymentScheduleInLoan } from 'calculate-tool';
repaymentScheduleInLoan({
	principal: 30000,
	rate: 0.6,
	periods: 4,
	isEqualPayment: true,
	decimal: 2
});
// return: 
/** [
 {"currentPeriod":1,"principal":7432.84,"interest":180,"repayment":7612.84},
 {"currentPeriod":2,"principal":7477.44,"interest":135.4,"repayment":7612.84},
 {"currentPeriod":3,"principal":7522.3,"interest":90.54,"repayment":7612.84},
 {"currentPeriod":4,"principal":7567.44,"interest":45.4,"repayment":7612.84}]
 **/

Parameters

NameDescriptionTypeAttributesDefault
principalThe Principle of the loanNumber
rateThe interst rate of every period;Number
periodsNumber of loan termsNumber
isEqualPaymentThe repayment method;If true, it's Fixed Repayment Loan; If false, it's Equal Principal Payment LoanBoolean\<optional>true
decimalReserved decimal places for resultsNumber\<optional>4

Returns

Type: Array\<Object> | Name | Description | Type | | :------ | :----------- | :------ | | currentPeriod| The current period of the loan | Number| |repayment|The Repayment amount for the current period of the loan|Number| |principal|The Principal for the current period of the loan|Number| |interest| The Interest for the current period of the loan|Number|

principalInLoan(Parameters)

Get the Princial for the current period of the loan

Examples

import { principalInLoan } from 'calculate-tool';
principalInLoan({
	principal: 30000,
	rate: 0.6,
	periods: 4,
	currentPeriod: 2,
	isEqualPayment: true,
	decimal: 2
}); 
// return:  7477.44

Parameters

NameDescriptionTypeAttributesDefault
principalThe Principle of the loanNumber
rateThe interst rate of every period;Number
periodsNumber of loan termsNumber
currentPeriodThe current period of the loanNumber
isEqualPaymentThe repayment method;If true, it's Fixed Repayment Loan; If false, it's Equal Principal Payment LoanBoolean\<optional>true
decimalReserved decimal places for resultsNumber\<optional>4

Returns

Type: Number

interestInLoan(Parameters)

Get the Interest for the current period of the loan

Examples

import { interestInLoan } from 'calculate-tool';
interestInLoan({
	principal: 30000,
	rate: 0.6,
	periods: 4,
	currentPeriod: 2,
	isEqualPayment: true,
	decimal: 2
});
// return:  135.4

Parameters

NameDescriptionTypeAttributesDefault
principalThe Principle of the loanNumber
rateThe interst rate of every period;Number
periodsNumber of loan termsNumber
currentPeriodThe current period of the loanNumber
isEqualPaymentThe repayment method;If true, it's Fixed Repayment Loan; If false, it's Equal Principal Payment LoanBoolean\<optional>true
decimalReserved decimal places for resultsNumber\<optional>4

Returns

Type: Number

totalInterestInLoan(Parameters)

Get the total interest amount of the loan

Examples

import { totalInterestInLoan } from 'calculate-tool';
totalInterestInLoan({
	principal: 30000,
	rate: 0.6,
	periods: 4,
	isEqualPayment: true,
	decimal: 2
}); 
// return:  451.36

Parameters

NameDescriptionTypeAttributesDefault
principalThe Principle of the loanNumber
rateThe interst rate of every period;Number
periodsNumber of loan termsNumber
isEqualPaymentThe repayment method;If true, it's Fixed Repayment Loan; If false, it's Equal Principal Payment LoanBoolean\<optional>true
decimalReserved decimal places for resultsNumber\<optional>4

Returns

Type: Number

mean(data, decimal)

Get the mean of a list of number

Examples

import { mean } from 'calculate-tool';
mean(
	[1,2,3,4,5,6,7,8,9,10],
	2
); 
// return:  5.5

Parameters

NameDescriptionTypeAttributesDefault
dataA list of numberArray\<Number>
decimalReserved decimal places for resultsNumber\<optional>4

Returns

Type: Number

variance(data, decimal)

Get the variance of a list of number

Examples

import { variance } from 'calculate-tool';
variance(
	[1,2,3,4,5,6,7,8,9,10],
	2
); 
// return: 8.25

Parameters

NameDescriptionTypeAttributesDefault
dataA list of numberArray\<Number>
decimalReserved decimal places for resultsNumber\<optional>4

Returns

Type: Number

stdDeviation(data, decimal)

Get the standard deviation of population

Examples

import { stdDeviation } from 'calculate-tool';
stdDeviation(
	[1,2,3,4,5,6,7,8,9,10],
	2
); 
// return: 2.87

Parameters

NameDescriptionTypeAttributesDefault
dataA list of numberArray\<Number>
decimalReserved decimal places for resultsNumber\<optional>4

Returns

Type: Number

varianceOfSample(data, decimal)

Get the variance of sample

Examples

import { varianceOfSample } from 'calculate-tool';
varianceOfSample(
	[1,2,3,4,5,6,7,8,9,10],
	2
); 
// return: 9.17

Parameters

NameDescriptionTypeAttributesDefault
dataA list of numberArray\<Number>
decimalReserved decimal places for resultsNumber\<optional>4

Returns

Type: Number

stdDeviationOfSample(data, decimal)

Get the standard deviation of sample

Examples

import { stdDeviationOfSample } from 'calculate-tool';
stdDeviationOfSample(
	[1,2,3,4,5,6,7,8,9,10],
	2
); 
// return: 3.03

Parameters

NameDescriptionTypeAttributesDefault
dataA list of numberArray\<Number>
decimalReserved decimal places for resultsNumber\<optional>4

Returns

Type: Number

semiDeviation(data, target, decimal)

Get the target/down deviation of sample

Examples

import { semiDeviation } from 'calculate-tool';
semiDeviation(
	[1,2,3,4,5,6,7,8,9,10],
	3,
	2
); 
// return: 4.01

Parameters

NameDescriptionTypeAttributesDefault
dataA list of numberArray\<Number>
targetThe target for the listNumber
decimalReserved decimal places for resultsNumber\<optional>4

Returns

Type: Number

stdNormalDistributionValue(zScore)

Get the standard normal distribution value by z-score

Examples

import { stdNormalDistributionValue } from 'calculate-tool';
stdNormalDistributionValue(1.24); //  0.89251
stdNormalDistributionValue(-1.24); //  0.10749
stdNormalDistributionValue(0);// 0.5

Parameters

NameDescriptionTypeAttributesDefault
zScorez-scoreNumber

Returns

Type: Number

bankersRounding(number, decimal)

get the value by banker's rounding method

rules How to round numbers with banker's rounding With the banker's rounding, when the number to be rounded (the rounding digit) is followed by 1, 2, 3, and 4, round the number down; when followed by 6, 7, 8, and 9, round the number up. However, for numbers followed by 5, the rounding direction is determined according to the number after 5:

When there is a number after 5, round up. When there is no number after 5: If it is an odd number before 5, round up. If it is an even number before 5, round down.

Learn more 👉🏻 banker's rounding

Examples

import { bankersRounding } from 'calculate-tool';
calculatejs.bankersRounding(5.234, 2); // 5.23
calculatejs.bankersRounding(5.235, 2); //  5.24
calculatejs.bankersRounding(5.245, 2); //  5.24
calculatejs.bankersRounding(5.2352, 2); // 5.24

Parameters

NameDescriptionTypeAttributesDefault
numberThe number to be roundedNumber
decimalReserved decimal places for resultsNumber\<optional>4

Returns

Type: Number

Test

To run the tests using Node.js from the root directory:

npm run test
1.0.2

5 months ago

1.0.1

5 months ago

1.0.0

5 months ago