// Copyright 2023 The AthanorLabs/atomic-swap Authors
// SPDX-License-Identifier: LGPL-3.0-only

package coins

import (
	"github.com/cockroachdb/apd/v3"
)

func roundToDecimalPlace(result *apd.Decimal, n *apd.Decimal, decimalPlace uint8) error {
	result.Set(n) // already optimizes result == n

	// Adjust the exponent to the rounding place, round, then adjust the exponent back
	increaseExponent(result, decimalPlace)
	_, err := decimalCtx.RoundToIntegralValue(result, result)
	if err != nil {
		return err
	}
	decreaseExponent(result, decimalPlace)
	_, _ = result.Reduce(result)
	return nil
}
