Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4568096
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:01:39+00:00 2026-05-21T19:01:39+00:00

I have these 2 values: decimal totalAmountDue = 1332.29m; short installmentCount = 3; I

  • 0

I have these 2 values:

decimal totalAmountDue = 1332.29m;
short installmentCount = 3;

I want to create 3 installments that have an even amount based on the totalAmountDue (extra pennies apply starting with the lowest installment number going to the highest installment number) using this class:

public class Installment
{
   public Installment( short installmentNumber, decimal amount )
   {
     InstallmentNumber = installmentNumber;
     Amount = amount;
   }

   public short InstallmentNumber { get; private set; }
   public decimal Amount { get; private set; }
}

The installments should be as follows:

{ InstallmentNumber = 1, Amount = 444.10m }
{ InstallmentNumber = 2, Amount = 444.10m }
{ InstallmentNumber = 3, Amount = 444.09m }

I am looking for an interesting way to create my 3 installments. Using a simple LINQ to objects method would be nice. I have been trying to understand more about functional programming lately and this seems like it could be a fairly good exercise in recursion. The only decent way I can think of doing this is with a traditional while or for loop at the moment…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T19:01:40+00:00Added an answer on May 21, 2026 at 7:01 pm

    There’s not a whole lot here that is “functional”. I would approach the problem like this:

    var pennies = (totalAmountDue * 100) % installmentCount;
    var monthlyPayment = totalAmountDue / installmentCount;
    var installments = from installment in Enumerable.Range(1, installmentCount)
                       let amount = monthlyPayment + (Math.Max(pennies--, 0m) / 100)
                       select new Installment(installment, amount);
    

    You might be able to work something out where you constantly subtract the previous payment from the total amount and do the division rounding up to the nearest penny. In F# (C# is too wordy for this) it might be something like:

    let calculatePayments totalAmountDue installmentCount =
      let rec getPayments l (amountLeft:decimal) = function
                | 0 -> l
                | count -> let paymentAmount = 
                             (truncate (amountLeft / (decimal)count * 100m)) / 100m
                           getPayments (new Installment(count, paymentAmount)::l)
                                       (amountLeft - paymentAmount)
                                       (count - 1)
      getPayments [] totalAmountDue installmentCount
    

    For those unfamiliar with F#, what that code is doing is setting up a recursive function (getPayments) and bootstrapping it with some initial values (empty list, starting values). Using match expressions it sets up a terminator (if installmentCount is 0) returning the list so far. Otherwise it calculates the payment amount and calls the recursive method adding the new installment to the front of the list, subtracting the payment amount from the amount left, and subtracting the count.

    This is actually building the list in reverse (adding on to the front each time), so we throw away the extra pennies (the truncate) and eventually it catches up with us so the penny rounding works as expected. This is obviously more math intensive than the add/subtract code above since we divide and multiply in every iteration. But it is fully recursive and takes advantage of tail recursion so we’ll never run out of stack.

    The trouble with C# here is that you want a sequence of installments and recursion and there’s no idiomatic built-in structure for doing that in C#. Here I’m using F#’s list which is immutable and O(1) operation to prepend.

    You could possibly build something using the Scan() method in the Reactive Extensions to pass state from once instance to another.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rails model that validates uniqueness of 2 form values. If these
I have one table that stores values with a point in time: CREATE TABLE
I have a need to display many numerical values in columns. These values need
Imagine I have these python lists: keys = ['name', 'age'] values = ['Monty', 42,
well i have this messages table with sample values like these: msg_id recipient_id read
I have a set of five boolean values. If more than one of these
I have these two pieces of code, wich one is more readable? foreach decimal
I have lat/long value in this format 5,141,456,024 -231,146,115 I want to convert these
We have proto messages that ideally would contain a decimal as a part of
There are multiple values I have been storing in ASP.NET configSections sections for each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.