gpt4 book ai didi

swift - 如何在 Swift 中用 Int 对 Double 进行简单的除法?

转载 作者:搜寻专家 更新时间:2023-10-30 22:07:04 28 4
gpt4 key购买 nike

查看有关此主题的各种帖子,但仍然没有运气。将 Double(或 Float)除以 Int 时,是否有简单的除法/转换方法?这是 Playground 返回和错误“Double is not convertible to UInt8”的一个简单示例。

    var score:Double = 3.00
var length:Int = 2 // it is taken from some an array lenght and does not return decimal or float

var result:Double = (score / length )

最佳答案

使用 var result:Double=(score/Double(length)) 将 int 转换为 double这将做的是在计算除法之前,它将创建一个新的 Double 变量,其中 int 位于括号内,因此构造函数类似于语法。

关于swift - 如何在 Swift 中用 Int 对 Double 进行简单的除法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29140161/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com