Skip to content

Releases: sindresorhus/Percentage

v3.0.0

02 Feb 09:34

Choose a tag to compare

Breaking

  • Percentage * and / now support scalar overloads, so expressions like 50% * 2 and 100% / 2 change behavior from percent-to-percent math to scalar math.
  • Compound assignments with scalars change accordingly, for example value *= 2 now doubles the percent instead of treating 2 as 2%.
  • postfix % now accepts all BinaryFloatingPoint types, which can cause ambiguity if another module defines its own postfix % for Float or CGFloat.

Improvements

You can now use Percentage directly with common SwiftUI, UIKit, and AppKit APIs that accept a fraction Double:

Text("Hello")
	.opacity(45%)
	.brightness(20%)
	.contrast(80%)
	.saturation(50%)
	.grayscale(100%)

Color.red.opacity(50%)

UIColor.red.withAlphaComponent(50%)

NSColor.red.withAlphaComponent(50%)

v2.0.0...v3.0.0

v2.0.0

10 Sep 10:14

Choose a tag to compare

What's New

Advanced Calculation Methods

// Create percentages from ratios
Percentage.from(100, of: 200) //=> 50%

// Find original values before changes
40%.originalValueBeforeIncrease(finalValue: 120) //=> 85.714...
12%.originalValueBeforeDecrease(finalValue: 106) //=> 120.454...

// Answer "x IS y% OF what?"
90%.isPercentOf(67) //=> 74.444...

// Calculate percentage changes
Percentage.change(from: 100, to: 150) //=> 50%

Range & Validation

50%.isWithinStandardRange  //=> true
150%.clamped(to: 0%...100%) //=> 100%

Numeric Extensions

100.increased(by: 20%) //=> 120
100.0.decreased(by: 20%) //=> 80.0

Modern Formatting

33.333%.formatted(decimalPlaces: 1) //=> "33.3%"
50.5%.formatted(.percent.precision(.fractionLength(1))) //=> "50.5%"

Swift Concurrency Ready

Sendable conformance - Now fully thread-safe and async/await compatible!


v1.1.0...v2.0.0

v1.2.0

27 Sep 17:01

Choose a tag to compare

  • Add .clampedZeroToHundred

v1.1.0...v1.2.0

v1.1.0

27 Dec 13:53

Choose a tag to compare

  • Make percentage of given value generic (#11) 6624346

v1.0.0...v1.1.0

v1.0.0

06 Jul 18:56

Choose a tag to compare

Breaking

  • Rename package from Percent to Percentage 76480b7

Improvements

  • Conform to ExpressibleByFloatLiteral, ExpressibleByIntegerLiteral, and Numeric 6ad1e1b
  • Accept more floatingpoint/integer types directly 0513895
  • Add static .random(in:) method (#9) ac33f86

Fixes

  • Fix multiplication and division for percentages 50a5144

v0.2.0...v1.0.0

v0.2.0

13 Nov 10:41

Choose a tag to compare

  • Localize the Percent description (#8) 8720f10

v0.1.1

03 Oct 05:36

Choose a tag to compare

v0.1.0

01 Oct 06:30

Choose a tag to compare

🦄