split expenses between people algorithm JS

huangapple go评论54阅读模式
英文:

problem split expenses between people algorithm JS

问题

我正在编写一个函数,将支出按照每个人实际支付的金额进行分配。但是,当我将1€分成3份时,返回的结果是每个参与者0.33€,我希望它是[0.33, 0.33, 0.34]。我该如何实现这个?

我尝试获取总额并查看结果的差异,但这并没有起作用。

英文:

I am making a function that divide expense between people in function what they have paid. But when I split 1€ into 3 people, the return is 0.33 each participant, I want to be
[0.33, 0.33, 0.34]
How can I make it

I was trying to get the total and view the difference of the result but It doesn't work

答案1

得分: 1

  • 以分为单位工作,以避免浮点数问题。
  • 取金额除以人数的地板值,然后分配余数。
const divide = (amount, n) => [...Array(n)].map((_, i) => 
  Math.floor(amount / n) + (i < amount % n)).reverse();
console.log(divide(100, 3));
console.log(divide(101, 3));
英文:
  • Work in cents to avoid floating point issues.
  • Take the floor of dividing the amount by the number of people, then distribute the remainder.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const divide = (amount, n) =&gt; [...Array(n)].map((_, i) =&gt; 
  Math.floor(amount / n) + (i &lt; amount % n)).reverse();
console.log(divide(100, 3));
console.log(divide(101, 3));

<!-- end snippet -->

答案2

得分: 1

你可以将这些部分相加,然后取最后一个部分的剩余值进行分配。

英文:

You could sum the parts and take for the last the rest of the sum to distribute.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const
    getParts = (value, length) =&gt; Array.from(
        { length },
        (s =&gt; (_, i) =&gt; {
            if (i + 1 === length) return s.toFixed(2);
            const v = (value / length).toFixed(2);
            s -= v;
            return v;            
        })(value)
    );

console.log(getParts(1, 3));

<!-- end snippet -->

答案3

得分: -1

a 为被除数,b 为除数,n 为您想要的小数位数。您可以尝试通过循环来解决正确的除法。当最后一个循环结束时,您应该从累积计数中减去总数。

let round = (num, n) => {
  return Math.round((num + Number.EPSILON) * Math.pow(10, n)) / Math.pow(10, n);
}

let divide = (a, b, n) => {
  let d = round(a / b, n);
  let res = [];
  for (let i = 0; i < b; i++) {
    res.push(i != b - 1 ? d : round(a - i * d, n));
  }
  return res;
}
console.log(divide(1, 3, 2))

如果您有任何其他问题,请随时提出。

英文:

Let a be the dividend, b be the divisor, and n be the number of decimal places you want. You can try to solve the correct division by looping it. When the last loop comes, you should subtract the total from the cumulative count

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let round = (num, n) =&gt; {
  return Math.round((num + Number.EPSILON) * Math.pow(10,n)) / Math.pow(10,n);
}

let divide = (a, b, n) =&gt; {
  let d = round(a/b, n);
  let res = [];
  for(let i=0; i &lt; b; i++) {
    res.push(i != b-1 ? d : round(a - i*d, n));
  }
  return res;
}
console.log(divide(1,3,2))

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月4日 07:29:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632682.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定