Swift Number Output

Swift Number Output

Print numeric values with print() and format using interpolation or format styles.


Printing Numbers

Print values directly or embed them using string interpolation with \(value).

Example

let a = 7, b = 3
print(a, b)                 // space-separated by default
print("a=\(a), b=\(b)")     // interpolation
print("sum = \(a + b)")     // inline math

This example prints numbers directly and via string interpolation, including inline addition.



Math Functions

Common functions like absmin, and max help compute numeric results.

  • abs(x) returns the absolute value of x.
  • min(x, y) returns the smaller of x and y.
  • max(x, y) returns the larger of x and y.

Example

let x = -7
print(abs(x))          // 7
print(min(3, 8))       // 3
print(max(3, 8))       // 8

This example shows absolute value, minimum, and maximum functions.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *