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

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

问题描述

我想在 Swift 中将 Float 转换为 Int.像这样的基本转换不起作用,因为这些类型不是基元,不像 Objective-C 中的 floats 和 ints

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'

知道如何将属性从 Float 转换为 Int 吗?

Any idea how to property convert from Float to Int?

推荐答案

您可以像这样在 Swift 中将 Float 转换为 Int :

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天全站免登陆