如何比较一个小数以上的数字

时间:2012-06-28 13:16:44

标签: iphone xcode variables comparison decimal

我正在尝试比较一个软件版本......让我们说5.4.9

companysettings.software是一个nsstring

if ([companysettings.software version //some type of value] < 5.4.9) {

//you need to update blah blah blah

} else {

//proceed with app flow

}

如何比较具有多个小数的数字?

6 个答案:

答案 0 :(得分:3)

IMO最好将组件分开并连续比较。使版本号为单个数字将引入错误的可能性(例如5.1.2 = 5.12)。

答案 1 :(得分:1)

这是更好的答案...... 在这种情况下,我正在寻找的版本是6.0.6 6.0.5将触发versionLower为TRUE

NSArray *versionStrings = [companySettings.softwareVersion componentsSeparatedByString:@"."];

BOOL versionLower = FALSE;

for (int i = 0; i < 3; i++) {

    switch (i) {
        case 0:

            if ([[versionStrings objectAtIndex:0] intValue] < 6) {
                versionLower = TRUE;
            }
            break;

        case 1:

            if ([[versionStrings objectAtIndex:1] intValue] < 0) {
                versionLower = TRUE;
            }
            break;

        case 2:

            if ([[versionStrings objectAtIndex:2] intValue] < 6) {
                versionLower = TRUE;
            }
            break;

        default:
            break;
    } 
}

if (!versionLower) {

//version of software is okay... proceed with flow

}

答案 2 :(得分:0)

//Suppose these are the versions
NSString *version1 = @"4.3.2";
NSString *version2 = @"3.2.1.8";

//split first two characters of your version
NSString *first2digitversion1 = [version1 substringToIndex:2];
NSString *first2digitversion2 = [version2 substringToIndex:2];

//now get remaining part of string
NSString *remainVersion1 = [version1 substringFromIndex:2];
NSString *remainVersion2 = [version2 substringFromIndex:2];

//now remove the "." from string
remainVersion1 = [remainVersion1 stringByReplacingOccurrencesOfString:@"." withString:@""];
remainVersion2 = [remainVersion2 stringByReplacingOccurrencesOfString:@"." withString:@""];

//now merge both
NSString *comPareStr1 = [[NSString stringWithFormat:first2digitversion1]stringByAppendingString:remainVersion1];
NSString *comPareStr2 = [[NSString stringWithFormat:first2digitversion2]stringByAppendingString:remainVersion2];

// now you will get 4.32 and 3.218 
//and this is the comparision
if([comPareStr1 floatValue] > [comPareStr2 floatValue])
{
    NSLog(@"comPareStr1 is greater");
}
else
{
    NSLog(@"comPareStr2 id greater");
}

答案 3 :(得分:0)

根据您的要求做一些修改并阻止对arrayIndexOfBoundException:但逻辑很容易理解..

 NSString *firstVersion = @"5.4.9";
 NSArray *firstChunks = [firstVersion componentsSeparatedByString: @"."];

 NSString *secondVersion = @"6.4.9";
 NSArray *secondChunks = [secondVersion componentsSeparatedByString: @"."];

  for(int i = 0 ; i< [firstChunks count] ; i++){
      int first = [[firstChunks objectAtIndex:i] intValue];
      int second = [[secondChunks objectAtIndex:i] intValue];
      if(first>second){
         NSLog(@"first version in heigher");
     }
     else
         NSLog(@"second version in heigher");
}

这可能会对你有所帮助

答案 4 :(得分:0)

这篇文章得到了我发现的最佳答案Compare version numbers in Objective-C

  

这是比较版本的最简单方法,请记住“1”   &LT; “1.0”&lt; “1.0.0”:

NSString* requiredVersion = @"1.2.0";
NSString* actualVersion = @"1.1.5";

if ([requiredVersion compare:actualVersion options:NSNumericSearch] == NSOrderedDescending) {
  // actualVersion is lower than the requiredVersion
}

答案 5 :(得分:-1)

这就是我做的......

从字符串中删除小数,然后比较

NSString *stringWithoutDecimals = [companySettings.softwareVersion stringByReplacingOccurrencesOfString:@"." withString:@""];

if ([stringWithoutDecimals intValue]  >= 606) {

}