All Projects → sindresorhus → Percentage

sindresorhus / Percentage

Licence: mit
A percentage type for Swift

Programming Languages

swift
15916 projects
type
21 projects

Projects that are alternatives of or similar to Percentage

Returns
Make your functions return something meaningful, typed, and safe!
Stars: ✭ 2,015 (+795.56%)
Mutual labels:  type-safety
Orm Lite
Header-Only, Strong-Typed, Compile-time Object Relation Mapping (ORM) in Modern C++ :-)
Stars: ✭ 164 (-27.11%)
Mutual labels:  type-safety
Swiftyfitsize
📱 Swifty screen adaptation solution (Support Objective-C and Swift)
Stars: ✭ 184 (-18.22%)
Mutual labels:  number
Domino Ui
Domino-ui
Stars: ✭ 138 (-38.67%)
Mutual labels:  type-safety
React Native Input Spinner
An extensible input number spinner component for react-native highly customizable. This component enhance a text input for entering numeric values, with increase and decrease buttons.
Stars: ✭ 155 (-31.11%)
Mutual labels:  number
Libphonenumber Js
A simpler (and smaller) rewrite of Google Android's libphonenumber library in javascript
Stars: ✭ 2,233 (+892.44%)
Mutual labels:  number
Javascript Number Formatter
Lightweight & Fast JavaScript Number Formatter
Stars: ✭ 119 (-47.11%)
Mutual labels:  number
Actual Number Picker
Android: A horizontal number picker
Stars: ✭ 206 (-8.44%)
Mutual labels:  number
Vue Literal Compiler
A Vue Compiler that allows you compile your string literals to render functions at build time and write components in SFC paradigm
Stars: ✭ 158 (-29.78%)
Mutual labels:  type-safety
Typestrict
ESLint config focused on maximizing type safety 💪
Stars: ✭ 182 (-19.11%)
Mutual labels:  type-safety
Ssf
📝 Spreadsheet Number Formatter
Stars: ✭ 139 (-38.22%)
Mutual labels:  number
Android Xnumberkeyboard
Android number keyboard view. Android自定义的数字键盘。
Stars: ✭ 149 (-33.78%)
Mutual labels:  number
Is Number
JavaScript/Node.js utility. Returns `true` if the value is a number or string number. Useful for checking regex match results, user input, parsed strings, etc.
Stars: ✭ 176 (-21.78%)
Mutual labels:  number
Hegel
An advanced static type checker
Stars: ✭ 1,804 (+701.78%)
Mutual labels:  type-safety
React Flip Numbers
🎰 Flip your numbers in 3D
Stars: ✭ 185 (-17.78%)
Mutual labels:  number
Typecov
Track missing type coverage to ensure type safety
Stars: ✭ 128 (-43.11%)
Mutual labels:  type-safety
Squid
Squid – type-safe metaprogramming and compilation framework for Scala
Stars: ✭ 172 (-23.56%)
Mutual labels:  type-safety
Sonataintlbundle
Symfony SonataIntlBundle
Stars: ✭ 212 (-5.78%)
Mutual labels:  number
Ts Toolbelt
ts-toolbelt is the largest, and most tested type library available right now, featuring +200 utilities. Our type collection packages some of the most advanced mapped types, conditional types, and recursive types on the market.
Stars: ✭ 3,099 (+1277.33%)
Mutual labels:  type-safety
Vntnumberpickerpreference
NumberPicker Preference for Android
Stars: ✭ 176 (-21.78%)
Mutual labels:  number

Percentage

A percentage type for Swift

Makes percentages more readable and type-safe, for example, for APIs that currently accept a fraction Double.

-.opacity(0.45)
+.opacity(45%)

Install

Add the following to Package.swift:

.package(url: "https://github.com/sindresorhus/Percentage", from: "1.1.0")

Or add the package in Xcode.

Usage

See the source for docs.

import Percentage

10% + 5.5%
//=> 15.5%

-10% / 2
//=> -5%

(40% + 93%) * 3
//=> 399%

50% * 50%
//=> 25%

30% > 25%
//=> true

50%.of(200)
//=> 100

Percentage(50)
//=> 50%

Percentage(fraction: 0.5)
//=> 50%

50%.fraction
//=> 0.5

10%.rawValue
//=> 10

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:

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].