Swift - Floating-Point Literals

Introduction

You can represent floating-point values as follows:

  • Hexadecimal: Use the 0x prefix

The following code shows the floating-point number 345.678 represented in the two forms:

Demo

let num5 = 345.678
let num6 = 3.45678E2    // 3.45678 x 10^2
let num7 = 34567.8E-2   // 3.45678 x 10^(-2)

The E or e represents the exponent.

3.45678E2 means 3.45678 times 10 to the power of two.

You can represent a hexadecimal floating-point number with an exponent of base 2:

Demo

let num8 = 0x2Cp3       // 44 x 2^3
let num9 = 0x2Cp-3      // 44 x 2^(-3)

Here, 2Cp3 means 2C (hexadecimal; which is 44 in decimal) times two to the power of three.

Related Topic