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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:30:35+00:00 2026-06-08T05:30:35+00:00

Web app coded in PHP with a MySQL database. I have a system which

  • 0

Web app coded in PHP with a MySQL database.

I have a system which calculates different costs for a number of people when splitting a cost. For example Person A buys something for 10 and Persons B, C, and D should split the cost.

The system should therefor register a positive record for person A of 10 and negative records of 10/3 for B, C and D.

However, when this is done; B, C and D all have -3.33 after rounding. Which of course doesn’t add up to the total of 10. What’s the best way of going about this problem? An optimal solution would randomise what person get’s the slightly bigger cost as well.

One possible solution is if I just let the last person’s debt be 10 - (A + B), but then there’s a problem if four persons split a cost of for example 13.34. The different parts would then be 3.34, 3.34, 3.34 and 3.32, whereas the optimal split would be 3.34, 3.34, 3.33, 3.33.

Some might argue that with sufficient decimals this is only a problem when having vast amounts of rows. But in an economical system I think it’s important to have a fail-safe system even from the start. It needs to be scalable, and can’t have even the slightest error. Unfairness is alright, just not errors.

Similar problem: sum divided values problem (dealing with rounding error)

  • 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-08T05:30:38+00:00Added an answer on June 8, 2026 at 5:30 am

    This seems to work – http://jsfiddle.net/nQakD/ .

    Used jQuery as example, but if you know PHP you should be able to easily convert it to PHP. If you need also php code, tell me, I will wrote it for you.

    I’ll paste the code here also –

    $(document).ready(function() {
        var price = 17.48, people = 4, payment = (price/people).toFixed(2), count=0;
        var payments = [];
        for(i = 0; i < people; i++) {
           payments.push(payment);   
        }
    
        if(payment*people != price) {
            var currentPayment = payment*people;
    
            $(payments).each(function() {
                if(currentPayment < price) {
                    currentPayment = (currentPayment-this).toFixed(2);
                    var newPayment = parseFloat(this)+0.01;
                    payments[count] = newPayment.toFixed(2);
                    currentPayment = parseFloat(currentPayment)+parseFloat(newPayment);
                }
                else if(currentPayment > price) {
                    currentPayment = (currentPayment-this).toFixed(2);
                    var newPayment = parseFloat(this)-0.01;
                    payments[count] = newPayment.toFixed(2);
                    currentPayment = parseFloat(currentPayment)+parseFloat(newPayment);
                }
                count++;
            });   
    
        }  
        $(payments).each(function() {
            $("#result").append("<b>"+this+"</b><br/>");
        });       
    });​
    

    EDIT:

    And here is working php code –

    $price = 13.34;
    $people = 4;
    $payment = (float)$price/$people;
    $payment = 0.01 * (int)($payment*100);
    $count = 0;
    $payments = Array();
    for($i = 0; $i < $people; $i++) {
        array_push($payments, $payment);
    }
    if($payment*$people != $price) {
        $currentPayment = $payment*$people;
        foreach($payments as $pay) {
            if($currentPayment < $price) {
                $currentPayment = $currentPayment-$pay;
                $currentPayment = 0.01 * (int)($currentPayment*100);               
                $newPayment = (float)$pay+0.01;
                $newPayment = 0.01 * (int)($newPayment*100);
                $payments[$count] = $newPayment;
                $currentPayment = (float)$currentPayment+$newPayment;
            }
            else if($currentPayment > $price) {
                $currentPayment = $currentPayment-$pay;
                $currentPayment = 0.01 * (int)($currentPayment*100);               
                $newPayment = (float)$pay-0.01;
                $newPayment = 0.01 * (int)($newPayment*100);
                $payments[$count] = $newPayment;
                $currentPayment = (float)$currentPayment+$newPayment;
            }
            $count++;
        }
    }
    foreach($payments as $payed) {
        echo '<b>'.$payed.'</b><br />';
    }​​​
    

    EDIT 2:

    This should fix the js issue – http://jsfiddle.net/nQakD/ updated code above also.

    EDIT 3:

    Edited PHP code and JS code so it does work for all examples – http://jsfiddle.net/nQakD/ .

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

Sidebar

Related Questions

I have a completed web app in PHP 5 + MySQL. I have not
i am develop the iOS web app using php and mysql database and my
I have lot of code in my Tornado app which looks like this: @tornado.web.asynchronous
I have a webapp written in PHP using a MySQL database backend. This question
I have a php based web app that on a user entering a value,
I have a web application written in PHP. It uses MySQL for data storage.
I have following in my config.php file for one of my web app. //db
I'm developing a web app. MySql/PHP back-end, and HTML/jQuery front-end. I wanted to use
I have a web app which performs a character encoding conversion on .ssa files
I want to create my very first web app using HTML, MySQL, PHP and

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.