当前位置: 动力学知识库 > 问答 > 编程问答 >

Swift Doble rounding up after 7 decimal places

问题描述:

I have a number that I am trying to append to an array. The number is a coordinate -37.77745068746633 however in my Swift project if I println the array after the append call the value that should be -37.77745068746633 is -37.77745069

I am receiving the number through Google snap to roads API and I can see the original as the full number but changes after I call

self.latitude = locations["latitude"] as! Double

Swift just doesn't seem to store the entire value. Is there rounding on by default?

Thanks

网友答案:

If you keep the number as Double all the time, Swift won't round it, it is most likely the print statement that truncates it to a reasonable precision.

By the way, try to check the distance between the original and the truncated coordinates:

let lat1: Double = -37.77745068746633
let lat2: Double = -37.77745069
let lon: Double = 150


let loc1 = CLLocation(latitude: lat1, longitude: lon)
let loc2 = CLLocation(latitude: lat2, longitude: lon)

print(loc1.distanceFromLocation(loc2)) // prints 0.000281218294500339

If I am not making any mistake, the difference is just 0.3 millimeters.

分享给朋友:
您可能感兴趣的文章:
随机阅读: