What is Double.description in Swift?

Overview

The Double.description property in Swift returns the textual representation of a double value.

Syntax

Double.description

Return value

This property returns a string value.

Code example

// Create some double values
let d1 = 2.34
let d2 = 5.565
let d3 = 0.1111
// Get the textual representation
let t1 = d1.description
let t2 = d2.description
let t3 = d3.description
// Print the text
print(t1)
print(t2)
print(t3)
// Print the object types of the text
print(type(of: t1))
print(type(of: t2))
print(type(of: t3))

Explanation

  • Lines 2 to 4: We create the double values we need.
  • Lines 7 to 9: We get the textual representations of the strings using the description.
  • Lines 12 to 14: The textual representations of the strings are printed to the console.
  • Lines 17 to 19: The textual representations’ object types are printed to the console.

Free Resources