在Swift中将Float转换为Int [英] Convert Float to Int in Swift

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

问题描述

我想在Swift中将 Float 转换为 Int 。这样的基本类型不起作用,因为这些类型不是原语,不像Objective-C中的 float s和 int

I want to convert a Float to an Int in Swift. Basic casting like this does not work because these types are not primitives, unlike floats and ints in Objective-C

var float: Float = 2.2
var integer: Int = float as Float

但这会产生以下错误讯息:

But this produces the following error message:


'Float'不能转换为'Int'

'Float' is not convertible to 'Int'

任何想法如何从 code>到 Int

推荐答案

code> Float 到Swift中的,如下所示:

You can convert Float to Int in Swift like this:

var myIntValue:Int = Int(myFloatValue)
println "My value is \(myIntValue)"

您也可以使用@ paulm的注释来实现此结果:

You can also achieve this result with @paulm's comment:

var myIntValue = Int(myFloatValue)

这篇关于在Swift中将Float转换为Int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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