swift - Printed double value differs from originally set value -


i adding 2 array contains double values

 let arraya = [1.1,2.3]  let arrayb = [4.2,5.5,6.8]   let c = arraya + arrayb   print("\(c)"); 

the print function printing following result :

[1.1000000000000001, 2.2999999999999998, 4.2000000000000002, 5.5, 6.7999999999999998] 

how exact double values after adding arrays ?

you don't. it's way double (and float) work.

as workaround can round values when going print them

for num in c {     print(string(format: "%.1f", num)) } 

1.1

2.3

4.2

5.5

6.8


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -