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

Function ApplyRate

model/model.go:390–409  ·  view source on GitHub ↗

ApplyRate applies the exchange rate to the precise amount and returns a *big.Int. The rate is applied after precision to maintain accuracy.

(preciseAmount *big.Int, rate float64)

Source from the content-addressed store, hash-verified

388// ApplyRate applies the exchange rate to the precise amount and returns a *big.Int.
389// The rate is applied after precision to maintain accuracy.
390func ApplyRate(preciseAmount *big.Int, rate float64) *big.Int {
391 if rate == 0 {
392 rate = 1
393 }
394
395 // Create a new big.Float from the precise amount
396 preciseAmountFloat := new(big.Float).SetInt(preciseAmount)
397
398 // Create a big.Float for the rate
399 rateFloat := new(big.Float).SetFloat64(rate)
400
401 // Multiply the amount by the rate
402 result := new(big.Float).Mul(preciseAmountFloat, rateFloat)
403
404 // Convert back to big.Int (rounding if necessary)
405 resultBigInt := new(big.Int)
406 result.Int(resultBigInt)
407
408 return resultBigInt
409}
410
411// validate checks if the transaction is valid (e.g., ensuring positive amount).
412func (transaction *Transaction) validate() error {

Callers 2

UpdateBalancesFunction · 0.85
TestApplyRateFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestApplyRateFunction · 0.68