Menu
CoddyTech

Numbers and Conversion

Lesson 3 of 48 in Coddy's Swift for Luau Developers course.

Luau number is a double-precision floating-point value. Division produces a numeric quotient, and math.floor explicitly rounds down. Integers beyond the exact representable range can lose distinctions: adding one to 2^53 yields the same number here. A number annotation does not introduce a separate exact integer type.

Swift Int and Double are distinct types. Integer division truncates toward zero, while Double conversion before division retains the fractional quotient. The shown 64-bit Int calculation distinguishes the two neighboring large integers. Overflow still traps for ordinary Int arithmetic, so use the stated bounds rather than assuming unlimited precision.

Luau

Complete Luau console program:

main.luau

local paint: number = 11
print(paint / 4)
print(math.floor(paint / 4))
print(9007199254740992 + 1 == 9007199254740992)

Swift

Complete program; compile the listed files together:

main.swift

import Foundation
let paint = 11
print(Double(paint) / 4.0)
print(paint / 4)
print(9007199254740992 + 1 == 9007199254740992)
challenge icon

Challenge

Easy

Implement clockParts(_ minutes: Int) -> String. Input is total minutes, 0 through 10000. Return whole hours, a colon, then remaining minutes, without zero padding.

Implement the required APIs and solve(_ input: String) -> String. Return output text without a final newline; do not print in editable code. The locked driver independently checks the APIs on additional valid inputs, then prints the returned text with exactly one newline. All numeric operations stay within Int bounds.

Your code goes in Functions.swift. main.swift holds the supplied input/output code and cannot be edited.

Try it yourself

import Foundation
func apiChecks(_ input: String) -> Bool {
return clockParts(181) == "3:1" && clockParts(60) == "1:0"
}
let input = String(data: FileHandle.standardInput.readDataToEndOfFile(), encoding: .utf8)!
// BEGIN PUBLIC API PROBES
let apiOK = apiChecks(input)
// END PUBLIC API PROBES
if apiOK { print(solve(input)) } else { print("API mismatch") }
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Swift for Luau Developers

Practice on your own: Swift playground