判断整数是否为一位并在其前面加零

Den*_*zar 1 string int swift

我需要检查 int 是否只有一位数字,如果是,我想在它前面添加一个零。我有这段代码,但它不起作用。

var minutes2 = Int(minutes)
var minutessize: Int = sizeofValue(minutes2)

if minutessize < 2 {
    var needStringHere = "0\(minutes2)"
    let a: Int? = needStringHere.toInt()
    minutes2 = a!
}
Run Code Online (Sandbox Code Playgroud)

ego*_*dan 5

您可以检查分钟数是否小于 10:

var str = "\(minutes2)"
if (minutes2 < 10) { str = "0\(minutes2)" }
Run Code Online (Sandbox Code Playgroud)