Swift中的数学除法 [英] Math divison in Swift

查看:310
本文介绍了Swift中的数学除法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用不同的方程式和公式制作一个数学应用程序,但是我试图圈出一个扇形,但是我只是想尝试将输入值除以360,但是当我这样做时,它只会显示0,除非该值已经超过360.我曾尝试过使用String,Double和Float的方法,但是没有运气,我不知道我在做什么是错误的,但是下面是代码.我很感谢您的帮助,但我已经坐了一会儿,在网上搜索了答案,但没有结果,可能是我搜索错误的结果.

I'm trying to make a math app with different equations and formulas but I'm trying to circle sector but i just wanted to try to divide the input value by 360 but when I do that it only says 0 unless the value is over 360. I have tried using String, Double and Float with no luck I don't know what I'm doing is wrong but down here is the code. I'm thankful for help but I have been sitting a while and searched online for an answer with no result I might have been searching with the wrong search.

if graderna.text == ""{
        }
        else{
            var myInt: Int? = Int(graderna.text!)    // conversion of string to Int
            var myInt2: Int? = Int(radien.text!)
            let pi = 3.1415926
            let lutning = 360


            let result = (Double(myInt! / lutning) * Double(pi))
            svar2.text = "\(result)"
        }

推荐答案

您的代码正在执行整数除法,将整数结果转换为双精度.相反,您想将这些单个整数转换为双精度,然后进行除法.例如.

Your code is performing integer division, taking the integer result and converting it to a double. Instead, you want to convert these individual integers to doubles and then do the division. E.g.

let result = Double(myInt!) / Double(lutning) * pi

或定义

let lutning = 360.0

然后

let result = Double(myInt!) / lutning * pi

而且,顺便说一句,我建议使用M_PI而不是定义自己的pi.

And, BTW, I'd suggest using M_PI rather than defining your own pi.

let result = Double(myInt!) / lutning * M_PI

这篇关于Swift中的数学除法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆