Releases: sindresorhus/Percentage
Releases · sindresorhus/Percentage
v3.0.0
Breaking
Percentage *and/now support scalar overloads, so expressions like50% * 2and100% / 2change behavior from percent-to-percent math to scalar math.- Compound assignments with scalars change accordingly, for example
value *= 2now doubles the percent instead of treating2as2%. postfix %now accepts allBinaryFloatingPointtypes, which can cause ambiguity if another module defines its own postfix%forFloatorCGFloat.
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
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.0Modern 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.2.0
- Add
.clampedZeroToHundred
v1.1.0
v1.0.0
Breaking
- Rename package from
PercenttoPercentage76480b7
Improvements
- Conform to
ExpressibleByFloatLiteral,ExpressibleByIntegerLiteral, andNumeric6ad1e1b - 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
v0.1.1
v0.1.0
🦄