// WithdrawPct subtracts a percentage of the balance. It returns the // quantity that has been deducted and an error or nil if the percentage // is not in the range 0..100. func (h *Resource) WithdrawPct(percentage float64) (us.Quantity, error) { if percentage < 0 || percentage > 100 { msg := fmt.Sprintf("percentage not in range 0..1") return us.Quantity{}, errors.New(msg) } taken := us.MultFac(h.balance, percentage/100.0) h.balance = us.Subtract(h.balance, taken) return h.Convert(taken), nil }
// Withdraw subtracts the given amount from the Resource. // Return true for success, false for incompatible unit or out of bounds func (h *Resource) Withdraw(q us.Quantity) bool { if !us.AreCompatible(h.balance, q) { return false } n := us.Subtract(h.balance, q) if h.outOfBounds(n) { return false } h.balance = n return true }