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 8916069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:08:24+00:00 2026-06-15T05:08:24+00:00

I am trying to divide a number into multiple parts so the sum of

  • 0

I am trying to divide a number into multiple parts so the sum of the part are equal to the input number.

If I have 3.99 and if I need to divide into two parts, the expected output is 2 and 1.99 (2+1.99=3.99)

And if I need to divide 3.99 into 3 parts, the expected output is 1.3, 1.3 and 1.39 (1.3+1.3+1.39=3.99)

I was able to output 2.99 and 1 for the first example and I was able to get 1.99, 1 and 1 for the second example. However, in both the cases results are very far from the expected output.

I need to split postal charges into separate line items. The result could be equally divided or unequally divided while making sure that the sum of the different parts is equal to the input number.

Here are some other examples:

3.99 divided in 3 parts: 1.33, 1.33, 1.33
3.98 divided in 3 parts: 1.33, 1.33, 1.32
3.97 divided in 3 parts: 1.32, 1.32, 1.33
3.96 divided in 3 parts: 1.32, 1.32, 1.32
3.95 divided in 3 parts: 1.32, 1.32, 1.31
  • 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-06-15T05:08:26+00:00Added an answer on June 15, 2026 at 5:08 am

    I tested with your supplied values. May need some tweaking:

    static IEnumerable<decimal> SplitValue2(decimal value, int count)
    {
        if (count <= 0) throw new ArgumentException("count must be greater than zero.", "count");
        var result = new decimal[count];
    
        decimal runningTotal = 0M;
        for (int i = 0; i < count; i++)
        {
            var remainder = value - runningTotal;
            var share = remainder > 0M ? Math.Max(Math.Round(remainder / ((decimal)(count - i)), 2), .01M) : 0M;
            result[i] = share;
            runningTotal += share;
        }
    
        if (runningTotal < value) result[count - 1] += value - runningTotal;
    
        return result;
    }
    

    This assumes you pass in a value with 2 decimal precision. If you pass in 3.999, you’re not going to get correct results.

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

Sidebar

Related Questions

If I need to divide for example 7 into random number of elements of
I'm currently trying to divide counts[email] a hash containing the number 82,000 by a
I'm trying to divide an integer N <= 12, which is a number of
I am trying to divide NSInteger with some number, but I am receiving errors.
I am trying to divide $_POST values into different arrays to check its values.
I am trying to divide my 2 number. And I determine them with different
I was trying to write predicate divide(L,Len,Slist) which will be true when Slist can
Using ExtJs. I'm trying to design a main which is divided into three sub
Trying to build out an exception if move.UserId does not equal currentUserId then Redirect
I have a large collection of documents scanned into PDF format, and I wish

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.