Struggling to understand the allocate
function
#423
-
The upgrade to 2.0.0 requires us to replace
const d1 = Dinero({ amount: 51998, currency: "GBP" })
const monthly = d1.divide(12)
console.log(monthly.toUnit()) // Results in: 43.33
const d1 = dinero({ amount: 51998, currency: GBP })
const allocation = allocate(d1, [1, 12])
const values = allocation.map((allot) => toUnit(allot))
console.log(values) // Results in: [ 40, 479.98 ] However, the result of this is not the result I'm after. How can I distribute the value into 12? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
Hey @JayyWalker! When using If you need to split your object into twelve equal parts, your code should look like this: const d1 = dinero({ amount: 51998, currency: GBP });
const allocation = allocate(d1, [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]); // an array of 12 ratios of value `1` (even distribution)
const values = allocation.map((allocated) => toUnit(allocated));
console.log(values); // [ 52, 52, 52, 52, 52, 52, 52, 52, 51.99, 51.99 ] As you can see, The long list of ratios is a bit unsightly, so you can refactor to: const ratios = Array.from({ length: 12 }).fill(1);
const allocation = allocate(d1, ratios); |
Beta Was this translation helpful? Give feedback.
-
Can we not add back in shortcuts or helper functions that will support more, and this is my opinion, common divide? |
Beta Was this translation helpful? Give feedback.
-
Is there a workaround to divide by an arbitrary number that could be a decimal? I've tried to multiply by a fraction, but this only works if the divisor is an integer. When it's a decimal, some error occurs within dinero and I don't receive any output beyond that point. For example, if num2 is an integer, I get an accurate result. If it's a decimal, I get no result and this won't console log. dineroResult = multiply(dinero1, 1 / num2);
console.log('divide result:', dineroResult); Seem very limiting to not support |
Beta Was this translation helpful? Give feedback.
Hey @JayyWalker!
When using
allocate
, you're splitting the value into "buckets". If you do CSS, you can see it as CSS Grids, when you distributegrid-template-columns
(1fr 1fr 2fr ...
).If you need to split your object into twelve equal parts, your code should look like this:
As you can see,
allocate
split the value into twelve parts, trying as much as possible to make them equa…