2013-06-13 45 views
0

我有代碼來比較cellforrowAtIndexPath中的float或double值。我正在比較它,並在評估比較值時將顏色設置爲tableView單元格。但有時候,值不同時它不會進入不等於條件的內部。可以通過任何正文告訴我什麼是我可以用來比較兩個值的正確方法float或double值。不等於條件(!=)的日誌有時不打印。比較float或double值的正確方法?

我的代碼如下:---

double br_preVal=[[[prevDict valueForKey:@"bestbuyprice"] valueForKey:@"text"] doubleValue]; 
double br_nxtVal=[[[tempDic valueForKey:@"bestbuyprice"] valueForKey:@"text"] doubleValue]; 

double sr_preVal=[[[prevDict valueForKey:@"bestsellprice"] valueForKey:@"text"] doubleValue]; 
double sr_nxtVal=[[[tempDic valueForKey:@"bestsellprice"] valueForKey:@"text"] doubleValue]; 


if (br_nxtVal!=br_preVal) { 

    NSLog(@"buy rate Not equal === and values %f,%f",br_preVal,br_nxtVal); 

    if (br_nxtVal>br_preVal) { 
     [mwCell.buyRate setBackgroundColor:t.socketHighbgColor]; 
     mwCell.buyRate.textColor=t.socketHighfgColor; 

     [celllc replaceObjectAtIndex:indexPath.row withObject:t.socketHighbgColor]; 
    }else{ 
     [mwCell.buyRate setBackgroundColor:t.socketLowbgColor]; 
     mwCell.buyRate.textColor=t.socketLowfgColor; 

     [celllc replaceObjectAtIndex:indexPath.row withObject:t.socketLowbgColor]; 
    } 
}else{ 

    mwCell.buyRate.backgroundColor=[celllc objectAtIndex:indexPath.row]; 
    mwCell.buyRate.textColor=([[celllc objectAtIndex:indexPath.row] isEqual:t.socketLowbgColor])?t.socketLowfgColor:t.socketHighfgColor; 
} 

if (sr_nxtVal!=sr_preVal) { 

    NSLog(@"sell rate Not equal === and values == %f,%f",sr_preVal,sr_nxtVal); 

    if (sr_nxtVal>sr_preVal) { 
     [mwCell.sellRate setBackgroundColor:t.socketHighbgColor]; 
     [mwCell.sellRate setTextColor:t.socketHighfgColor]; 

     [cellrc replaceObjectAtIndex:indexPath.row withObject:t.socketHighbgColor]; 
    }else{ 
     [mwCell.sellRate setBackgroundColor:t.socketLowbgColor]; 
     [mwCell.sellRate setTextColor:t.socketLowfgColor]; 

     [cellrc replaceObjectAtIndex:indexPath.row withObject:t.socketLowbgColor]; 
    } 
}else{ 

    mwCell.sellRate.backgroundColor=[cellrc objectAtIndex:indexPath.row]; 
    mwCell.sellRate.textColor=([[cellrc objectAtIndex:indexPath.row] isEqual:t.socketLowbgColor])?t.socketLowfgColor:t.socketHighfgColor; 
} 

mwCell.buyRate.text=[NSString stringWithFormat:@"%.2f",br_nxtVal]; 
mwCell.sellRate.text=[NSString stringWithFormat:@"%.2f",sr_nxtVal]; 

浮點值有一些這樣的事2396.250000

+0

排在首位,我不認爲你需要使用浮點數作爲標識符...的[是多麼危險 – 2013-06-13 06:01:44

+0

可能重複的比較浮點值?](http://stackoverflow.com/questions/10334688/how-dangerous-is-it-to-compare-floatingpoint-values) –

+0

@MatthiasBauch我知道,但這些答案不起作用對我來說,我發佈它。 – Warewolf

回答

1

嘗試這樣

#define kVerySmallValue (0.000001) 
    if(fabsf(br_nxtVal - br_preVal) < kVerySmallValue) { 
    //// Your code 
    } 
+0

謝謝,看來你的代碼正在工作 – Warewolf

+0

但是,它幾次跳過條件它的數據來得很快 – Warewolf

0

比較浮點值,最好的方法是使用範圍

if(4.0<=a&&a<=4.5) 

或者您可以使用fabs(number)<comparator>fabs(number)

+0

也使用晶圓廠,這也似乎不能正常工作 – Warewolf

+0

蠻力使用abs()或類型轉換爲int – amar

1

通過Deafult比較,像3.13小數淡水河谷double所以如果你有把它分配給在float varibale做到這一點:

float f = 3.14f;//now it is float 

看看這個例子:

float f = 3.14; 
if(f == 3.14) 
{ 
    NSLog(@"Equal"); 
} 
else 
{ 
    NSLog(@"Not Equal"); 
} 

它將打印不等於...

因此,要做到這一點,最好的辦法是:

float f = 3.14; 
    if(f == 3.14f) 
    { 
     NSLog(@"Equal"); 
    } 
    else 
    { 
     NSLog(@"Not Equal"); 
    }