convertDecimalToPrecise converts a decimal amount to precise integer by multiplying by precision
(transaction *Transaction)
| 366 | // convertDecimalToPrecise converts a decimal amount to precise integer |
| 367 | // by multiplying by precision |
| 368 | func 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. |
no outgoing calls
no test coverage detected