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

Function PrecisionBankersRound

model/transaction.go:106–127  ·  view source on GitHub ↗

PrecisionBankersRound applies banker's rounding (round half to even) at the given precision scale factor.

(num float64, precision float64)

Source from the content-addressed store, hash-verified

104
105// PrecisionBankersRound applies banker's rounding (round half to even) at the given precision scale factor.
106func PrecisionBankersRound(num float64, precision float64) float64 {
107 // // For standard 2-decimal precision, use the original bankersRound
108 // if precision == 100 {
109 // return bankersRound(num)
110 // }
111
112 // Directly use the precision as the scale factor
113 // This is more accurate than calculating decimal places with log10
114 shifted := num * precision
115 whole := math.Floor(shifted)
116 fraction := shifted - whole
117
118 // Apply banker's rounding logic (round half to even)
119 if math.Abs(fraction-0.5) < 1e-10 {
120 if math.Mod(whole, 2) == 0 {
121 return whole / precision // Round down for even
122 }
123 return (whole + 1) / precision // Round up for odd
124 }
125
126 return math.Round(shifted) / precision
127}
128
129// GetEffectiveDate returns the transaction's effective date, falling back to CreatedAt if not explicitly set.
130func (t *Transaction) GetEffectiveDate() time.Time {

Callers 1

Calls

no outgoing calls

Tested by 1