Iphone ios如何检查除法余数是否为整数

Iphone ios如何检查除法余数是否为整数,iphone,ios,xcode,ipad,Iphone,Ios,Xcode,Ipad,你们中的任何人都知道如何检查除法余数是整数还是零 if ( integer ( 3/2)) 听起来您正在寻找的是模运算符%,它将为您提供操作的剩余部分 3 % 2 // yields 1 3 % 1 // yields 0 3 % 4 // yields 1 但是,如果您希望首先实际执行除法,则可能需要更复杂的操作,例如: //Perform the division, then take the remainder modulo 1, which will //yield any deci

你们中的任何人都知道如何检查除法余数是整数还是零

if ( integer ( 3/2))

听起来您正在寻找的是模运算符
%
,它将为您提供操作的剩余部分

3 % 2 // yields 1
3 % 1 // yields 0
3 % 4 // yields 1
但是,如果您希望首先实际执行除法,则可能需要更复杂的操作,例如:

//Perform the division, then take the remainder modulo 1, which will
//yield any decimal values, which then you can compare to 0 to determine if it is
//an integer
if((a / b) % 1 > 0))
{
    //All non-integer values go here
}
else
{
    //All integer values go here
}
演练

(3 / 2) // yields 1.5
1.5 % 1 // yields 0.5
0.5 > 0 // true

你应该像这样使用模运算符

// a,b are ints
if ( a % b == 0) {
  // remainder 0
} else
{
  // b does not divide a evenly
}
Swift 2.0

print(Int(Float(9) % Float(4)))   // result 1
swift 3:

if a.truncatingRemainder(dividingBy: b) == 0 {
    //All integer values go here
}else{
    //All non-integer values go here
}

您可以使用下面的代码来了解它是哪种类型的实例

var val = 3/2
var integerType = Mirror(reflecting: val)

if integerType.subjectType == Int.self {
  print("Yes, the value is an integer")
}else{
  print("No, the value is not an integer")
}

请告诉我上述内容是否有用。

Swift 5

if numberOne.isMultiple(of: numberTwo) { ... }
Swift 4或以下

if numberOne % numberTwo == 0 { ... }

使用%模除,它给出余数。我的问题是谁知道余数是整数还是零。例如,3/2提醒不是零或整数:如果((10%2)%1>0){NSLog(@“提醒%d”,(10/2));},但没有work@RionWilliams真聪明@Juan,我对Xcode(或Objective-C)不太熟悉,但我会为您进一步研究。@Juan,这可能是通过NSLog输出小数时需要如何格式化的问题吗?我相信Juan正在尝试实际执行除法(包括任何小数余数)并确定结果是否为整数。