Saturday, June 28, 2014

Integers and Floating-Point Numbers


let implicitInteger = 50
let implicitDouble = 50.0
//Do not declare the type

let explicitDouble: Double = 50

let explicitFloat: Float = 5
//Floating-point number 5.0 is created.
//Declare the type

Type Casting
let x = Float(implicitInteger) * 1.5
//x = 75.0

Extra Underscores for readability
let oneThousand = 1_000
//oneThousand = 1,000

var y = 3 //OK
var y = 4 //Error
//Each constant / variable can be declared once only.

More on println calculation:

let chicken = 2
let rabbit = 4
println("One chicken and two rabbits have \(chicken*1 + rabbit*2) legs.")

//Console output: One chicken and two rabbits have 10 legs.

No comments:

Post a Comment