Swift Float Data Type

In Swift, Float is a floating-point data type used to store numbers with a fractional component. It uses single-precision storage and is suitable when approximately six decimal digits of precision are sufficient.

Examples of values that can be stored in a Float include 3.14, -8.5, and 0.001. For calculations that require greater precision, Swift generally uses the Double type.

Declare a Float Variable in Swift

To declare a variable that stores a Float, specify Float after the variable name.

</>
Copy
  var variableName :Float

A declaration without an initial value must usually be followed by an assignment before the variable is read.

Initialize a Swift Float Value

You can initialize the variable by assigning a floating-point value while declaring it. The explicit Float annotation prevents Swift from inferring the value as a Double.

</>
Copy
  var pi :Float = 3.14159

You can use let instead of var when the value should not change.

</>
Copy
let temperature: Float = 26.5

Print a Float Value in Swift

Use the print() function to write a Float value to the console.

main.swift

</>
Copy
var pi :Float = 3.14159 
print(pi)

Output

3.14159

Create a Float with Float.init()

Swift supports type inference, but a decimal literal without an explicit type is normally inferred as Double. Calling the Float initializer creates a value whose type is specifically Float.

main.swift

var pi = Float(3.14159)
print(pi)
print(type(of:pi))

Output

3.14159
Float

Difference Between 2 and 2.0 in Swift

The literal 2 is an integer literal, while 2.0 is a floating-point literal. When no type context is provided, Swift commonly infers 2 as Int and 2.0 as Double.

</>
Copy
let wholeNumber = 2
let decimalNumber = 2.0
let floatNumber: Float = 2.0

print(type(of: wholeNumber))
print(type(of: decimalNumber))
print(type(of: floatNumber))

Output

Int
Double
Float

Perform Arithmetic with Swift Float Values

Swift supports the standard arithmetic operators for Float values, including addition, subtraction, multiplication, and division.

</>
Copy
let first: Float = 10.5
let second: Float = 2.0

print(first + second)
print(first - second)
print(first * second)
print(first / second)

Output

12.5
8.5
21.0
5.25

The operands of an arithmetic expression must have compatible types. Swift does not automatically combine an Int, Float, and Double in the same calculation.

Convert Int and Double Values to Float

Use the Float initializer when converting an integer or a Double to Float.

</>
Copy
let itemCount = 12
let preciseValue = 9.87654321

let countAsFloat = Float(itemCount)
let valueAsFloat = Float(preciseValue)

print(countAsFloat)
print(valueAsFloat)

Converting from Double to Float can reduce precision because Float stores fewer significant digits.

Swift Float vs Double

Float is a 32-bit, single-precision floating-point type. Double is a 64-bit, double-precision type and can represent more significant digits.

FeatureFloatDouble
Storage32 bits64 bits
Approximate decimal precisionAbout 6 decimal digitsAbout 15 decimal digits
Default type for decimal literalsNoYes
Typical useAPIs or data structures that specifically require 32-bit valuesGeneral floating-point calculations

Use Double for general-purpose calculations unless an API, file format, graphics operation, or memory constraint specifically requires Float.

Understand Float Precision in Swift

Floating-point values are stored using a binary representation. As a result, many decimal fractions cannot be represented exactly. This can produce small rounding differences during calculations.

</>
Copy
let first: Float = 0.1
let second: Float = 0.2
let result = first + second

print(result)

Do not rely on exact equality when two values were produced by separate floating-point calculations. Compare their difference against a small tolerance instead.

</>
Copy
let expected: Float = 0.3
let actual: Float = 0.1 + 0.2
let tolerance: Float = 0.000001

if abs(actual - expected) < tolerance {
    print("Values are approximately equal")
}

Format a Swift Float to Two Decimal Places

Use String(format:) when you need a display string with a fixed number of decimal places. Import Foundation to use this initializer.

</>
Copy
import Foundation

let price: Float = 19.4567
let formattedPrice = String(format: "%.2f", price)

print(formattedPrice)

Output

19.46

Formatting changes the displayed string; it does not permanently change the value stored in the Float.

Check Special Float Values

Swift floating-point types can represent special values such as positive infinity, negative infinity, and NaN, which means “not a number.”

</>
Copy
let positiveInfinity = Float.infinity
let invalidNumber = Float.nan

print(positiveInfinity.isInfinite)
print(invalidNumber.isNaN)

Output

true
true

Common Swift Float Errors

  • Assuming every decimal literal is a Float: Swift normally infers an unannotated decimal literal as Double.
  • Mixing numeric types directly: Convert Int, Float, or Double operands to a compatible type before calculating.
  • Expecting exact decimal arithmetic: Binary floating-point calculations can contain small rounding differences.
  • Using Float when greater precision is required: Prefer Double for calculations that need more significant digits.
  • Using Float for exact currency calculations: Consider a decimal representation or integer minor units when values must remain exact.

Swift Float Frequently Asked Questions

What is a Float in Swift?

A Float is a 32-bit floating-point data type used to store positive or negative numbers with fractional components. It provides approximately six decimal digits of precision.

Is 2.0 a Float or Double in Swift?

Without type context, Swift normally infers 2.0 as a Double. Declare it as let value: Float = 2.0 or use Float(2.0) when you specifically need a Float.

Should I use Float or Double in Swift?

Use Double for most general calculations because it provides greater precision. Use Float when an API or data format requires a 32-bit floating-point value or when reduced storage is a deliberate requirement.

How do I convert a Double to Float in Swift?

Pass the Double value to the Float initializer, as in let floatValue = Float(doubleValue). Be aware that the conversion can lose precision.

How do I display a Float with two decimal places?

After importing Foundation, use String(format: "%.2f", value). This returns a formatted string rounded to two decimal places for display.

Swift Float Review Checklist

  • Confirm that each decimal literal intended to be a Float has an explicit type annotation or initializer.
  • Check that arithmetic operands use compatible Swift numeric types.
  • Use tolerance-based comparisons for calculated floating-point values.
  • Verify that Float provides enough precision for the calculation.
  • Keep numeric storage separate from two-decimal-place display formatting.

Summary of the Swift Float Data Type

In this Swift Tutorial, we learned how to declare, initialize, print, convert, calculate with, compare, and format Float values. We also examined the difference between Float and Double and the precision limits of floating-point arithmetic.