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

Function convertDecimalToPrecise

model/model.go:368–386  ·  view source on GitHub ↗

convertDecimalToPrecise converts a decimal amount to precise integer by multiplying by precision

(transaction *Transaction)

Source from the content-addressed store, hash-verified

366// convertDecimalToPrecise converts a decimal amount to precise integer
367// by multiplying by precision
368func convertDecimalToPrecise(transaction *Transaction) *big.Int {
369 // We should avoid float multiplication due to precision loss
370 // Convert the components to strings first and use the decimal package
371
372 // Using decimal package approach
373 amountStr := strconv.FormatFloat(transaction.Amount, 'f', -1, 64)
374 precisionStr := strconv.FormatFloat(transaction.Precision, 'f', 0, 64)
375
376 amountDec, _ := decimal.NewFromString(amountStr)
377 precisionDec, _ := decimal.NewFromString(precisionStr)
378
379 preciseAmount := amountDec.Mul(precisionDec)
380
381 // Convert to big.Int
382 result := new(big.Int)
383 result.SetString(preciseAmount.String(), 10)
384
385 return result
386}
387
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.

Callers 1

applyPrecisionLogicFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected