DecimalExtensions.swift 399 B

12345678910111213141516
  1. import Foundation
  2. extension Decimal {
  3. func clamp(to pickerSetting: PickerSetting) -> Decimal {
  4. max(min(self, pickerSetting.max), pickerSetting.min)
  5. }
  6. }
  7. extension Collection where Element == Decimal {
  8. /// Returns the arithmetic mean, or zero if empty.
  9. var mean: Decimal {
  10. guard !isEmpty else { return .zero }
  11. return reduce(.zero, +) / Decimal(count)
  12. }
  13. }