types: add truncate quote quantity method

This commit is contained in:
c9s 2023-08-05 01:59:04 +08:00
parent 7060fd4ecb
commit 7d4d2f3e41
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -71,6 +71,19 @@ func (m Market) TruncateQuantity(quantity fixedpoint.Value) fixedpoint.Value {
return fixedpoint.MustNewFromString(qs)
}
// TruncateQuoteQuantity uses the tick size to truncate floating number, in order to avoid the rounding issue
func (m Market) TruncateQuoteQuantity(quantity fixedpoint.Value) fixedpoint.Value {
var ts = m.TickSize.Float64()
var prec = int(math.Round(math.Log10(ts) * -1.0))
var pow10 = math.Pow10(prec)
qf := math.Trunc(quantity.Float64() * pow10)
qf = qf / pow10
qs := strconv.FormatFloat(qf, 'f', prec, 64)
return fixedpoint.MustNewFromString(qs)
}
// RoundDownQuantityByPrecision uses the volume precision to round down the quantity
// This is different from the TruncateQuantity, which uses StepSize (it uses fewer fractions to truncate)
func (m Market) RoundDownQuantityByPrecision(quantity fixedpoint.Value) fixedpoint.Value {