Getting the decimal part of a double in Swift

前端 未结 9 1457
小鲜肉
小鲜肉 2020-11-28 12:37

I\'m trying to separate the decimal and integer parts of a double in swift. I\'ve tried a number of approaches but they all run into the same issue...

let x:         


        
9条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-28 13:01

    It's impossible to create a solution that will work for all Doubles. And if the other answers ever worked, which I also believe is impossible, they don't anymore.

    let _5678 = 1234.5678.description.drop { $0 != "." } .description // ".5678"
    Double(_5678)  // 0.5678
    
    let _567 = 1234.567.description.drop { $0 != "." } .description // ".567"
    Double(_567) // 0.5669999999999999
    

提交回复
热议问题