Madhur
Madhur

Reputation: 1116

Multiply a number in swift with more than 10 digits

I want to perform a multiplication like as shown below:

let a = 10
let b = a * (1e + 9)

how can I perform so??

and after multiplication, how can I show the result in this very format?

like b = 1e + 10

Upvotes: 2

Views: 519

Answers (1)

David Pasztor
David Pasztor

Reputation: 54706

When you use the scientific notation to declare a numeric literal, its type is inferred to be Double, so you need to convert it to Int to be able to multiply an Int with your numeric literal (or the other way around if you actually expect a Double result).

Also make sure that there are no spaces in your scientific notation (you can also leave out the +).

let ten = 10
let multiplied = ten * Int(1e+9)

In case the scientific literal is actually a Double, convert the Int to a Double, not the other way around:

let one = Double(ten) * 1e-1

Upvotes: 2

Related Questions