divide number with decimals javascript

前端 未结 8 2105
误落风尘
误落风尘 2021-01-03 12:54

how can I divide number (money) to x number equally the number could be with one or two decimal or without it

such as 1000 or 100.2 or

8条回答
  •  Happy的楠姐
    2021-01-03 13:41

    review of other answers

    After re-examining the solutions provided here, I noticed that they produce some strange results.

    @GeorgeMauler's divideCurrencyEqually appears to work with the inputs provided in the original question. But if you change them up a bit, it produces a very strange result for …

    // it appears to work
    divideCurrencyEvenly(10,3)
    => [ '3.33', '3.33', '3.34' ]
    
    // wups ...
    divideCurrencyEvenly(10,6)
    => [ '1.67', '1.67', '1.67', '1.67', '3.32' ]
    
    // hmm this one works ...
    divideCurrencyEvenly(18,5)
    => [ '3.60', '3.60', '3.60', '3.60', '3.60' ]
    
    // wups ...
    divideCurrencyEvenly(100,7)
    => [ '14.29', '14.29', '14.29', '14.29', '14.29', '28.55' ]
    

    While @Barmar's solution seems to perform a bit better … well a little bit …

    // it appears to work
    divide(10,3)
    => [ '3.33', '3.33', 3.34 ]
    
    // acceptable, i guess
    divide(10,6)
    => [ '1.66', '1.66', '1.66', '1.66', '1.66', 1.700000000000001 ]
    
    // hmm, not so good, but still acceptable
    divide(18,5)
    => [ '3.60', '3.60', '3.60', '3.60', 3.5999999999999996 ]
    
    // as acceptable as the last two, i guess
    divide(100,7)
    => [ '14.28', '14.28', '14.28', '14.28', '14.28', '14.28', 14.320000000000007 ]
    

    Numbers like 3.5999999999999996 are mostly forgivable because that's how float arithmetic works in JavaScript.

    But, what I find most disturbing about divide is that it's giving a mixed-type array (string and float). And because the numbers aren't rounded (to the correct precision) in the output, if you were to add the result up on paper, you will not arrive back at your original numerator input — that is, unless you do the rounding on your result.


    and we're still fighting for equality …

    My last grievance exists for both of the above solutions too. The result does not represent a distribution that is as close to equal as is possible.

    If you divide 100 by 7 with a precision of 2, @Barmar's solution (with the rounding problem fixed) would give …

    [ '14.28', '14.28', '14.28', '14.28', '14.28', '14.28', 14.32 ]
    

    If these were people and the numbers represented monies, 1 person shouldn't pay 4 pennies more. Instead 4 people should pay 1 penny more …

    [ 14.29, 14.29, 14.29, 14.29, 14.28, 14.28, 14.28 ]
    

    That's as close to equal as is possible


    back to square one

    I was dissatisfied with the solutions, so I made one of my own, distribute.

    It has 3 parameters: precision p, divisor d, and numerator n.

    • it's returns a homogenous array — always an Array of Number
    • if you add them up, you will get a value exactly equal to your original numerator

    It scales the numerator and finds the largest integer q where q * d <= n. Using modular division we know how many "slices" need to contribute q+1. Lastly, each q or q+1 is scaled back down and populates the output array.

    const fill = (n, x) =>
      Array (n) .fill (x)
    
    const concat = (xs, ys) =>
      xs .concat (ys)
    
    const quotrem = (n, d) =>
      [ Math .floor (n / d)
      , Math .floor (n % d)
      ]
    
    const distribute = (p, d, n) =>
    { const e =
        Math .pow (10, p)
        
      const [ q, r ] =
        quotrem (n * e, d)
        
      return concat
               ( fill (r, (q + 1) / e)
               , fill (d - r, q / e)
               )
    }
    
    console .log
      ( distribute (2, 3, 10)
        // [ 3.34, 3.33, 3.33 ]
        
      , distribute (2, 6, 10)
        // [ 1.67, 1.67, 1.67, 1.67, 1.66, 1.66 ]
    
      , distribute (2, 5, 18)
        // [ 3.6, 3.6, 3.6, 3.6, 3.6 ]
    
      , distribute (2, 7, 100)
        // [ 14.29, 14.29, 14.29, 14.29, 14.28, 14.28, 14.28 ]
      )

    You'll see that I made precision a parameter, p, which means you can control how many decimal places come out. Also note how the largest difference Δ between any number in the result is Δ <= 1/10^p

    distribute (0, 7, 100)
    => [ 15, 15, 14, 14, 14, 14, 14 ] // Δ = 1
    
    distribute (1, 7, 100)
    => [ 14.3, 14.3, 14.3, 14.3, 14.3, 14.3, 14.2 ] // Δ = 0.1
    
    distribute (2, 7, 100)
    => [ 14.29, 14.29, 14.29, 14.29, 14.28, 14.28, 14.28 ] // Δ = 0.01
    
    distribute (3, 7, 100)
    => [ 14.286, 14.286, 14.286, 14.286, 14.286, 14.285, 14.285 ] // Δ = 0.001
    
    distribute (4, 7, 100)
    => [ 14.2858, 14.2857, 14.2857, 14.2857, 14.2857, 14.2857, 14.2857 ] // Δ = 0.0001
    

    distribute can be partially applied in meaningful ways. Here's one way petty people could use it to precisely split the bill at a restaurant …

    // splitTheBill will use precision of 2 which works nice for monies
    const splitTheBill = (people, money) =>
      distribute (2, people, money)
    
    // partyOfThree splits the bill between 3 people
    const partyOfThree = money =>
      splitTheBill (3, money)
    
    // how much does each person pay ?
    partyOfThree (67.89)
    => [ 18.93, 18.93, 18.92 ]
    

    And here's an effective way to divide people into groups — while being careful not to divide an individual person — which typically results in death …

    // p=0 will yield only whole numbers in the result
    const makeTeams = (teams, people) =>
      distribute (0, teams, people)
    
    // make 4 teams from 18 people
    // how many people on each team?
    makeTeams (4, 18)
    => [ 5, 5, 4, 4 ]
    

提交回复
热议问题