https://github.com/sindresorhus/Percentage
A percentage type for Swift
https://github.com/sindresorhus/Percentage
number percent percentage swift-package type type-safety
Last synced: 20 days ago
JSON representation
A percentage type for Swift
- Host: GitHub
- URL: https://github.com/sindresorhus/Percentage
- Owner: sindresorhus
- License: mit
- Created: 2019-10-01T06:20:00.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-07-23T17:46:44.000Z (9 months ago)
- Last Synced: 2025-03-22T00:13:53.096Z (24 days ago)
- Topics: number, percent, percentage, swift-package, type, type-safety
- Language: Swift
- Homepage:
- Size: 25.4 KB
- Stars: 273
- Watchers: 2
- Forks: 23
- Open Issues: 5
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
- fucking-awesome-swift - Percentage - Make percentages more readable and type-safe. (Libs / Utility)
- awesome-swift - Percentage - Make percentages more readable and type-safe. (Libs / Utility)
README
# Percentage
> A percentage type for Swift
Makes percentages more readable and type-safe, for example, for APIs that currently accept a fraction `Double`.
```diff
-.opacity(0.45)
+.opacity(45%)
```## Install
Add the following to `Package.swift`:
```swift
.package(url: "https://github.com/sindresorhus/Percentage", from: "1.2.0")
```[Or add the package in Xcode.](https://developer.apple.com/documentation/xcode/adding_package_dependencies_to_your_app)
## Usage
See the [source](Sources/Percentage/Percentage.swift) for docs.
```swift
import Percentage10% + 5.5%
//=> 15.5%-10% / 2
//=> -5%(40% + 93%) * 3
//=> 399%50% * 50%
//=> 25%30% > 25%
//=> true50%.of(200)
//=> 100Percentage(50)
//=> 50%Percentage(fraction: 0.5)
//=> 50%50%.fraction
//=> 0.510%.rawValue
//=> 10110%.clampedZeroToHundred
//=> 100%print("\(1%)")
//=> "1%"Percent.random(in: 10%...20%)
//=> "14.3%"
```The type conforms to `Hashable`, `Codable`, `RawRepresentable`, `Comparable`, `ExpressibleByFloatLiteral`, `ExpressibleByIntegerLiteral`, `Numeric`, and supports all the arithmetic operators.
### Codable
The percentage value is encoded as a single value:
```swift
struct Foo: Codable {
let alpha: Percentage
}let foo = Foo(alpha: 1%)
let data = try! JSONEncoder().encode(foo)
let string = String(data: data, encoding: .utf8)!print(string)
//=> "{\"alpha\":1}"
```## FAQ
#### Can you support Carthage and CocoaPods?
No, but you can still use Swift Package Manager for this package even though you mainly use Carthage or CocoaPods.
## Related
- [Regex](https://github.com/sindresorhus/Regex) - Swifty regular expressions
- [Defaults](https://github.com/sindresorhus/Defaults) - Swifty and modern UserDefaults
- [Preferences](https://github.com/sindresorhus/Preferences) - Add a preferences window to your macOS app
- [KeyboardShortcuts](https://github.com/sindresorhus/KeyboardShortcuts) - Add user-customizable global keyboard shortcuts to your macOS app
- [LaunchAtLogin](https://github.com/sindresorhus/LaunchAtLogin) - Add "Launch at Login" functionality to your macOS app
- [More…](https://github.com/search?q=user%3Asindresorhus+language%3Aswift)