Printed double value differs from originally set value

后端 未结 3 441
慢半拍i
慢半拍i 2021-01-25 15:52

I am adding two array which contains double values

 let arrayA = [1.1,2.3]
 let arrayB = [4.2,5.5,6.8]

 let c = arrayA + arrayB

 print(\"\\(c)\");
         


        
3条回答
  •  北恋
    北恋 (楼主)
    2021-01-25 16:36

    You don't. It's the way Double (and Float) does work.

    As workaround you can round your values when you are going to print them

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

    1.1

    2.3

    4.2

    5.5

    6.8

提交回复
热议问题