MCPcopy Create free account
hub / github.com/devaccuracy/ledgerforge / adjustPercentageAmounts

Function adjustPercentageAmounts

model/transaction.go:234–256  ·  view source on GitHub ↗

adjustPercentageAmounts adjusts percentage amounts to maintain the correct total

(state *distributionState)

Source from the content-addressed store, hash-verified

232
233// adjustPercentageAmounts adjusts percentage amounts to maintain the correct total
234func adjustPercentageAmounts(state *distributionState) {
235 if len(state.percentAmounts) == 0 {
236 return
237 }
238
239 hundredDec := decimal.NewFromInt(100)
240 targetTotal := state.totalAmountDec.Mul(state.totalPercentage).Div(hundredDec)
241
242 currentTotal := decimal.Zero
243 for _, amount := range state.percentAmounts {
244 currentTotal = currentTotal.Add(amount)
245 }
246
247 diff := targetTotal.Sub(currentTotal)
248 if diff.Abs().Cmp(decimal.NewFromInt(0)) <= 0 {
249 return
250 }
251
252 largestKey, largestAmount := findLargestDecimal(state.percentAmounts)
253 if largestKey != "" && largestAmount.Sign() > 0 {
254 state.percentAmounts[largestKey] = state.percentAmounts[largestKey].Add(diff)
255 }
256}
257
258// findLargestDecimal finds the key with the largest decimal value
259func findLargestDecimal(amounts map[string]decimal.Decimal) (string, decimal.Decimal) {

Callers 1

Calls 1

findLargestDecimalFunction · 0.85

Tested by

no test coverage detected