Skip to content

Instantly share code, notes, and snippets.

@puls
Last active August 29, 2015 14:12
Show Gist options
  • Select an option

  • Save puls/cef53c8acc64e0c1e89c to your computer and use it in GitHub Desktop.

Select an option

Save puls/cef53c8acc64e0c1e89c to your computer and use it in GitHub Desktop.
Swift CGFloat
#import <UIKit/UIKit.h>
[NSString stringWithFormat:@"%.0f", 1.23]; // @"1"
[NSString stringWithFormat:@"%.0f", (double)1.23]; // @"1"
[NSString stringWithFormat:@"%.0f", (float)1.23]; // @"1"
[NSString stringWithFormat:@"%.0f", (CGFloat)1.23]; // @"1"
import UIKit
String(format: "%.0f", 1.23) // "1"
String(format: "%.0f", Double(1.23)) // Same thing as above, "1"
String(format: "%.0f", Float(1.23)) // "1"
String(format: "%.0f", CGFloat(1.23)) // "0"
@puls
Copy link
Author

puls commented Dec 30, 2014

TIL: CGFloat in Swift is not a typealias but a struct:

struct CGFloat {

    /// The native type used to store the CGFloat, which is Float on
    /// 32-bit architectures and Double on 64-bit architectures.
    typealias NativeType = Double
    init()
    init(_ value: Float)
    init(_ value: Double)

    /// The native value.
    var native: NativeType
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment