Rounding Floats and Doubles in Swift

Rounding is a common task when working with numbers in Swift and displaying information to the user in iOS and macOS apps. This post walks through examples for rounding the a specific decimal place, rounding up, and rounding down in Swift:

  1. Rounding in Swift
  2. Rounding 1 Decimal Place (Tenths)
  3. Rounding 2 Decimal Places (Hundredths)
  4. Rounding 3 Decimal Places (Thousandths)
  5. A Function For Precision Rounding
  6. Rounding Up Using ceil
  7. Rounding Down Using floor

FREE all Swift loading animations.

Loading Animation Example

Follow AdvancedSwift and immediately get access to commented code for these animations that you can use in your projects.

Rounding in Swift

When using Swift for macOS and iOS apps, round(_:) is a built-in function able to accept a Double or a Float. The round function will round to the nearest whole number:

let double: Double = 1.95
let roundedDouble = round(double)
// roundedDouble is 2.0

let float: Float = 3.42
let roundedFloat = round(float)
// roundedFloat is 3.0

Rounding 1 Decimal Place (Tenths)

The built-in round(_:) function does not have an interface to round to a specific decimal place. To round to the tenths place, multiple the value by 10 then divide by 10.0:

let value = 5.4873
let roundedValue = round(value * 10) / 10.0
// roundedValue is 5.5

Rounding 2 Decimal Places (Hundredths)

The built-in round(_:) function does not have an interface to round to a specific decimal place. To round to the hundredths place, multiple the value by 100 then divide by 100.0:

let value = 5.4873
let roundedValue = round(value * 100) / 100.0
// roundedValue is 5.49

Rounding 3 Decimal Places (Thousandths)

The built-in round(_:) function does not have an interface to round to a specific decimal place. To round to the thousandths place, multiple the value by 1000 then divide by 1000.0:

let value = 5.4873
let roundedValue = round(value * 1000) / 1000.0
// roundedValue is 5.487

A Function For Precision Rounding

Instead of having multiple instances of round(value * 100) / 100.0 rounding in a codebase, a clean interface for rounding can be implemented:

// Examples
preciseRound(1.27) // Result is 1
preciseRound(1.27, precision: .tenths) // Result is 1.3

// Specify the decimal place to round to using an enum
public enum RoundingPrecision {
    case ones
    case tenths
    case hundredths
}

// Round to the specific decimal place
public func preciseRound(
    _ value: Double, 
    precision: RoundingPrecision = .ones) -> Double 
{
    switch precision {
    case .ones:
        return round(value)
    case .tenths:
        return round(value * 10) / 10.0
    case .hundredths:
        return round(value * 100) / 100.0
    }
}

Rounding Up Using ceil

To round a Double or Float up to the nearest whole number use the built-in function ceil:

let value = 1.23
ceil(value) // Result is 2.0

Like round(_:), using multiplication and division to round up using ceil to a specific decimal place:

// Round up 1 decimal place (tenths)
let value = 1.23
ceil(value * 10) / 10.0 // Result is 1.3

Rounding Down Using Floor

To round a Double or Float down to the nearest whole number use the built-in function floor:

let value = 1.23
floor(value) // Result is 1.0

Like round(_:), using multiplication and division to round down using floor to a specific decimal place:

// Round down 1 decimal place (tenths)
let value = 1.23
floor(value * 10) / 10.0 // Result is 1.2

Rounding Numbers in Swift

That’s it! By using round(_:), ceil(_:), and floor(_:) you can round Double and Float values to any number of decimal places in Swift.