Swift Double isLess()

isLess() 方法用于检查一个数字是否小于另一个数字。

示例

// check is 9 is less than 3 or not var result = 9.isLess(than: 10) print(result)
// Output: true

isLess() 语法

isLess() 方法的语法如下:

num.isLess(than: otherNumber)

这里,num 是一个数字。


isLess() 参数

isLess() 方法接受一个参数:

  • otherNumber - 要测试的值

isLess() 返回值

isLess() 方法返回一个布尔值:

  • true - 如果 num 小于 otherNumber
  • false - 如果 num 大于 otherNumber

示例 1:Swift Double isLess()

// check if 4 less than 2 or not
var result1 = 4.isLess(than: 2)
print(result1) // check if 2 is less than 4 or not
var result2 = 2.isLess(than: 4)
print(result2) // check if 210 is less than 110 or not
var result3 = 210.isLess(than: 110)
print(result3)

输出

false
true
false

在此示例中,由于 **4** 不小于 **2**,因此 isLess() 方法返回 false。但是,**2** 小于 **4**,该方法返回 true

最后,由于 **210** 是 **110** 的倍数,该方法返回 false


示例 2:在 if...else 中使用 isLess()

// check if 88 is less than 88.2 or not if 88.isLess(than: 88.2) {
print("The condition is true") } else { print("The condition is false") }

输出

 
The condition is true

在此示例中,由于 **88** 小于 **88.2**,因此执行了 if 块中的代码。

你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战