money2

Searchβ¦

Allocation

Allocation According to Ratios

Let our company have made a profit of 5 cents, which has to be divided amongst a company (70%) and an investor (30%). Cents can't be divided, so We can't give 3.5 and 1.5 cents. If we round up, the company gets 4 cents, the investor gets 2, which means we need to conjure up an additional cent.

The best solution to avoid this pitfall is to use allocation according to ratios.

1

import 'money2.dart';

2

final profit = Money.fromBigInt(BigInt.from(5), usd); // 5Β’

3

β

4

var allocation = profit.allocationAccordingTo([70, 30]);

5

assert(allocation[0] == Money.fromBigInt(BigInt.from(4), usd)); // 4Β’

6

assert(allocation[1] == Money.fromBigInt(BigInt.from(1), usd)); // 1Β’

7

β

8

// The order of ratios is important:

9

allocation = profit.allocationAccordingTo([30, 70]);

10

assert(allocation[0] == Money.fromBigInt(BigInt.from(2), usd)); // 2Β’

11

assert(allocation[1] == Money.fromBigInt(BigInt.from(3), usd)); // 3Β’

Copied!

Allocation to N Targets

An amount of money can be allocated to N targets using

`allocateTo()`

.1

import 'money2.dart';

2

final value = Money.fromBigInt(BigInt.from(800), usd); // $8.00

3

β

4

final allocation = value.allocationTo(3);

5

assert(allocation[0] == Money.fromBigInt(BigInt.from(267), usd)); // $2.67

6

assert(allocation[1] == Money.fromBigInt(BigInt.from(267), usd)); // $2.67

7

assert(allocation[2] == Money.fromBigInt(BigInt.from(266), usd)); // $2.66

Copied!

Last modified 1yr ago

Copy link

Contents