本文介绍了在 Swift 中将 Float 转换为 Int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在 Swift 中将 Float
转换为 Int
.像这样的基本转换不起作用,因为这些类型不是基元,不像 Objective-C 中的 float
s 和 int
s
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 float
s and int
s in Objective-C
var float: Float = 2.2
var integer: Int = float as Float
但这会产生以下错误消息:
But this produces the following error message:
'Float' 不能转换为 '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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!