Swiftpack.co -  Package - Gerzer/FloatConverter
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
Easy conversions between `Float`/`Double` and `CGFloat` in Swift
.package(url: "https://github.com/Gerzer/FloatConverter.git", from: "1.0.0")


FloatConverter offers a simple unary prefix operator ~ for easily converting between Float/Double and CGFloat without verbose, explicit initializer invocations.

let double = 3.14 // Of type `Double` through type inference
let cgFloat = CGFloat(double)

~double == cgFloat // --> `True`
~cgFloat == double // --> `True`
type(of: ~double) // --> `CGFloat.self`
type(of: ~cgFloat) // --> `Double.self`

For extra flexibility, the conversion to CGFloat is overloaded such that it accepts any type that conforms to the CGFloatConvertable protocol, which adopts the same requirements as the existing BinaryFloatingPoint protocol. This means that you can add conformance to your own custom types. Just make sure not to add conformance to CGFloat itself so that the compiler can catch redundant/unnecessary usage of the FloatConverter ~ operator.

For simplicity, the conversion from CGFloat always returns a Double. If you want to propose a way to choose to return a Float instead, then please open a GitHub issue (if one doesn't already exist).

CGFloat is implemented as a 32-bit Float on 32-bit platforms but as a 64-bit Double on 64-bit platforms, so the FloatConverter ~ operator might perform a lossy narrowing conversion depending on the system bit width. Of course, FloatConverter won't work in environments in which Core Graphics (and hence CGFloat) isn't available.


Stars: 1
Last commit: 5 weeks ago

Release Notes

Version 1.0.0
5 weeks ago

Initial release

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco