## DEV Community

Danilo Assis for Woovi

Posted on • Updated on

# How to calculate the largest remainder with Javascript

With a focus on innovating and continuing to scale our product catalogues, Woovi decided in the first half of 2023 to bring solutions that does not exist for Pix yet.

With this, we can provide different ways for our merchants to sell beyond the pix in sight.

With that in mind, we launched Pix Crediário and Woovi Parcelado. Two installments products for Pix Instant Payment in Brazil.

But what do they have in common?

Both work with installments and thinking in even numbers that's ok. But when we get to odd numbers, the world of installments can be a bit tricky.

But that already has a solution. We will teach you how we use the Largest Remainder Method algorithm to calculate our installments correctly without any surprise for the final customer.

## Largest Remainder Method

The "Largest Remainder" method is a technique used to distribute amounts proportionally, as in the case of installments.

Let's imagine we want to calculate an order, bill, sell of 100\$ in 3 installments. Only dividing by 3 is not enough. We can't set three installments of 33,33\$.

Then, we need to calculate the bigger value between then and set as our first installment.

To calculate this in js you can follow the code below:

``````const largestRemainder = (amount: number, numParts: number) => {
const individualAmount = Math.floor(amount / numParts);
const remainders = [];
let totalRemainder = 0;

// Calculate the remainders and total remainder
for (let i = 0; i < numParts; i++) {
const remainder = amount - (individualAmount * numParts) + individualAmount;
remainders.push(remainder);
totalRemainder += remainder;
}

// Distribute the total remainder proportionally among parts
for (let i = 0; i < numParts; i++) {
const proportion = remainders[i] / totalRemainder;
remainders[i] = Math.floor(proportion * totalRemainder);
}

return remainders;
}

const totalAmount = 100; // Total amount to be divided
const numParts = 3;      // Number of parts/parcels

const parcelAmounts = largestRemainder(totalAmount, numParts);
console.log(parcelAmounts); // An array containing the calculated parcel amounts
``````

In this example, the largestRemainder function calculates the installments based on the total amount (100\$) and the number of installments (3). It distributes the difference between the sum of the individual shares and the total value proportionally among the shares to ensure that the sum of the shares equals the total value. The result will be an array with the values of the calculated plots.

Keep in mind that this is a rough approximation and in some cases there may be small rounding differences. For more complex scenarios, consider using more advanced math libraries or more accurate calculation methods.

If you want to work in a startup in its early stages, This is your chance. Apply today!

Visit us Woovi!

Foto de Mediamodifier na Unsplash

Jonathan

Awesome post.

Which math libraries would you consider for that?
Also, there is a small typo in the function definition:

Change:

``````const largestRemainder => (amount: number, numParts: number)
``````

For:

``````const largestRemainder = (amount: number, numParts: number)
``````