我有代碼來比較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
排在首位,我不認爲你需要使用浮點數作爲標識符...的[是多麼危險 – 2013-06-13 06:01:44
可能重複的比較浮點值?](http://stackoverflow.com/questions/10334688/how-dangerous-is-it-to-compare-floatingpoint-values) –
@MatthiasBauch我知道,但這些答案不起作用對我來說,我發佈它。 – Warewolf