Prince Kumar Sharma
Prince Kumar Sharma

Reputation: 12641

Right way to compare float or double values?

I have code to compare float or double values in cellforrowAtIndexPath. I am comparing it and setting color to tableView cell on evaluating compare value.But some times , it is not getting inside the not equal to condition while value is different.Can any body tell me what is the right way I can use to compare two float or double values.The log inside not equal to condition(!=) is not printing sometimes.

My code is as follows:---

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];

float values are some thing like this 2396.250000

Upvotes: 0

Views: 448

Answers (3)

Prateek Prem
Prateek Prem

Reputation: 1544

By Deafult, a decimal vale like 3.13 is double so if you have to assign it to in float varibale do this:

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

see this Example:

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

it will print not Equal..

So the best way to do this is:

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

Upvotes: 1

DivineDesert
DivineDesert

Reputation: 6954

Try comparing like this

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

Upvotes: 1

amar
amar

Reputation: 4345

The best way to compare float values is to use range

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

or you can use fabs(number)< comparator >fabs(number)

Upvotes: 0

Related Questions