Home > Back-end >  javascript multiplication yields a different result
javascript multiplication yields a different result

Time:09-27

I am getting the rentInPeriod as result of monthsInPeriod * monthlyRent , as you can see on the result object below , as you can see on the object below for example monthlyRent = 108704 and monthsInPeriod = 12 the result should not be 1300828 , same with the remaining objects.

result should be the same on the sheet , the total should be $18,098,798 enter image description here

I basically just did let rentInPeriod = (monthsInPeriod * monthlyRent); pretty simple but I have no idea why it is yielding a different result .

If you dont mind I have posted my stackblitz for the full code. Thank you.

#object result from computation

[
    {
        "description": "Current Term",
        "monthlyRent": 53359,
        "monthsInPeriod": 0,
        "rentInPeriod": 0
    },
    {
        "description": "Current Term - Rent Adjustment",
        "monthlyRent": 108704,
        "monthsInPeriod": 12,
        "rentInPeriod": 1300828
    },
    {
        "description": "Current Term - Rent Adjustment",
        "monthlyRent": 110365,
        "monthsInPeriod": 12,
        "rentInPeriod": 1320696
    },
    {
        "description": "Current Term - Rent Adjustment",
        "monthlyRent": 117395,
        "monthsInPeriod": 12,
        "rentInPeriod": 1404827
    },
    {
        "description": "Current Term - Rent Adjustment",
        "monthlyRent": 119156,
        "monthsInPeriod": 12,
        "rentInPeriod": 1425906
    },
    {
        "description": "Current Term - Rent Adjustment",
        "monthlyRent": 120971,
        "monthsInPeriod": 12,
        "rentInPeriod": 1447617
    },
    {
        "description": "Current Term - Rent Adjustment",
        "monthlyRent": 122840,
        "monthsInPeriod": 12,
        "rentInPeriod": 1469980
    },
    {
        "description": "Current Term - Rent Adjustment",
        "monthlyRent": 124764,
        "monthsInPeriod": 12,
        "rentInPeriod": 1493013
    },
    {
        "description": "Current Term - Rent Adjustment",
        "monthlyRent": 132599,
        "monthsInPeriod": 12,
        "rentInPeriod": 1586770
    },
    {
        "description": "Current Term - Rent Adjustment",
        "monthlyRent": 134641,
        "monthsInPeriod": 12,
        "rentInPeriod": 1611207
    },
    {
        "description": "Current Term - Rent Adjustment",
        "monthlyRent": 136745,
        "monthsInPeriod": 12,
        "rentInPeriod": 1636376
    },
    {
        "description": "Current Term - Rent Adjustment",
        "monthlyRent": 138911,
        "monthsInPeriod": 12,
        "rentInPeriod": 1662301
    },
    {
        "description": "Current Term - Rent Adjustment",
        "monthlyRent": 141142,
        "monthsInPeriod": 12,
        "rentInPeriod": 1689003
    }
]

#stackblitz

https://stackblitz.com/edit/typescript-5ah2td

#ts code

const terminationPayment = 15000000;
const totalBrokerCommission = 150000;
const effectiveDate = new Date('12/1/2021');

function Compute(scheduleData: any): any {
  let startDate =
    typeof scheduleData.startDate === 'string'
      ? new Date(scheduleData.startDate)
      : scheduleData.startDate;
  const endDate =
    typeof scheduleData.endDate === 'string'
      ? new Date(scheduleData.endDate)
      : scheduleData.endDate;
  if (!startDate || startDate.toString() === 'Invalid Date') {
    startDate = effectiveDate;
  }
  let monthlyRent = scheduleData.annualRent / 12;
  let range360: any =
    startDate <= effectiveDate && endDate > effectiveDate
      ? Days360(effectiveDate, endDate)
      : startDate > effectiveDate && endDate > effectiveDate
      ? Days360(startDate, endDate)
      : 0;
  let monthsInPeriod = (range360 / 360) * 12;
  let rentInPeriod = monthsInPeriod * monthlyRent;
  return {
    description: scheduleData.description,
    monthlyRent: Math.round(monthlyRent),
    monthsInPeriod: Math.round(monthsInPeriod),
    rentInPeriod: Math.round(rentInPeriod),
  };
}

function Days360(
  startDate: Date,
  endDate: Date,
  decimalPlace: number = 2
): number | undefined {
  if (!startDate || !endDate) {
    return undefined;
  }
  let startMonth = startDate.getMonth();
  let startDayOfMonth = startDate.getDate();
  let startYear = startDate.getFullYear();
  let endMonth = endDate.getMonth();
  let endDayOfMonth = endDate.getDate();
  let endYear = endDate.getFullYear();

  let monthsDifference = endMonth - startMonth;
  let dayOfMonthDifference = endDayOfMonth - startDayOfMonth;
  let yearDifference = endYear - startYear;

  let monthsDifferenceInDate360 = 30 * monthsDifference;
  let yearDifferenceInDate360 = 360 * yearDifference;

  let differenceSum =
    yearDifferenceInDate360   monthsDifferenceInDate360   dayOfMonthDifference;
  return parseFloat(parseFloat(differenceSum.toString()).toFixed(decimalPlace));
}
var scheduleData = [
  {
    noticeDate: '-',
    description: 'Current Term',
    startDate: '',
    endDate: '11/30/2021',
    annualRent: 640306.56,
  },
  {
    noticeDate: '-',
    description: 'Current Term - Rent Adjustment',
    startDate: '12/01/2021',
    endDate: '11/30/2022',
    annualRent: 1304451,
  },
  {
    noticeDate: '-',
    description: 'Current Term - Rent Adjustment',
    startDate: '12/01/2022',
    endDate: '11/30/2023',
    annualRent: 1324375.32,
  },
  {
    noticeDate: '-',
    description: 'Current Term - Rent Adjustment',
    startDate: '12/01/2023',
    endDate: '11/30/2024',
    annualRent: 1408740,
  },
  {
    noticeDate: '-',
    description: 'Current Term - Rent Adjustment',
    startDate: '12/01/2024',
    endDate: '11/30/2025',
    annualRent: 1429877.6400000001,
  },
  {
    noticeDate: '-',
    description: 'Current Term - Rent Adjustment',
    startDate: '12/01/2025',
    endDate: '11/30/2026',
    annualRent: 1451649.48,
  },
  {
    noticeDate: '-',
    description: 'Current Term - Rent Adjustment',
    startDate: '12/01/2026',
    endDate: '11/30/2027',
    annualRent: 1474074.48,
  },
  {
    noticeDate: '-',
    description: 'Current Term - Rent Adjustment',
    startDate: '12/01/2027',
    endDate: '11/30/2028',
    annualRent: 1497172.2000000002,
  },
  {
    noticeDate: '-',
    description: 'Current Term - Rent Adjustment',
    startDate: '12/01/2028',
    endDate: '11/30/2029',
    annualRent: 1591190.28,
  },
  {
    noticeDate: '-',
    description: 'Current Term - Rent Adjustment',
    startDate: '12/01/2029',
    endDate: '11/30/2030',
    annualRent: 1615694.6400000001,
  },
  {
    noticeDate: '-',
    description: 'Current Term - Rent Adjustment',
    startDate: '12/01/2030',
    endDate: '11/30/2031',
    annualRent: 1640934.12,
  },
  {
    noticeDate: '-',
    description: 'Current Term - Rent Adjustment',
    startDate: '12/01/2031',
    endDate: '11/30/2032',
    annualRent: 1666930.92,
  },
  {
    noticeDate: '05/31/2033',
    description: 'Current Term - Rent Adjustment',
    startDate: '12/01/2032',
    endDate: '11/30/2033',
    annualRent: 1693707.4799999997,
  },
];

var remainingObligation = 0;
scheduleData.forEach(function (sd): void {
  var { rentInPeriod } = Compute(sd);
  console.log('rentInPeriod4443', { rentInPeriod });
  remainingObligation  = rentInPeriod;
});
console.log(remainingObligation);
const terminationPaymentPercent = terminationPayment / remainingObligation;
console.log(terminationPaymentPercent);
const terminationPaymentPercentAssumptions =
  (terminationPayment   totalBrokerCommission) / remainingObligation;
console.log(terminationPaymentPercentAssumptions);

CodePudding user response:

You're doing the calculations off of non-rounded numbers, but you store them rounded.

In your example, monthsInPeriod is around 11.9666667, and the rent is 108,704.25, giving you 1300828.

  • Related