Simple COUNTIF does not work

jimmylavino

New Member
Joined
Aug 29, 2011
Messages
1
Hello,

I have a very strange problem with Excel 2003 running under Windows 7 64-bit. I am using a very simple COUNTIF statement to count the occurrences of calculated values. For some reason, COUNTIF does not return the expected results.

For example, I have the following data:

Cell A1: 5
Cell B1: 4.9
Cell C1: =A1-B1

Cell C1 returns "0.1".

In Cell A4 I have a COUNTIF formula of "=COUNTIF(C1:C1, 0.1)". This returns "0", when I expected "1".

If I manually enter "0.1" in Cell C1 instead of relying on the formula, COUNTIF correctly returns "1".

If I change Cell B1 from "4.9" to "4.8" (and restore the formula under Cell C1 to return "0.2") and change my COUNTIF to "=COUNTIF(C1:C1, 0.2)", it correctly returns "1".

What am I doing wrong? I tried this on two other computers running Excel 2003 and in Google Docs and get the same result. I assume it is a syntax problem, but can't figure out what.

Thanks in advance.

:(
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Welcome to the board...

It's not you, it's a floating point precision problem (search google for explaination of it)

Change your formula in C1 to round the result..
=ROUND(A1-B1,1)

Change the 1 to the number of significant digits you want the result rounded to.

Hope that helps.
 
Upvote 0
Very strange.. I was able to recreate the same problem.

Instead of countif, I used IF(C1=0.1,"yes","no") and the result was "no".

I then used IF(C1<0.1,"yes","no") and the result was "yes".

If C1 was 0.1 instead of a formula, then it was fine. For some reason, if the result of a subtraction is 0.1, excel is treating it is slightly less than 0.1.

Somebody else on the forum might know if this is a known bug.
 
Upvote 0
Very strange.. I was able to recreate the same problem.

Instead of countif, I used IF(C1=0.1,"yes","no") and the result was "no".

I then used IF(C1<0.1,"yes","no") and the result was "yes".

If C1 was 0.1 instead of a formula, then it was fine. For some reason, if the result of a subtraction is 0.1, excel is treating it is slightly less than 0.1.

Somebody else on the forum might know if this is a known bug.

As jonmo1 has pointed out, it's the well-known floating point precision problem.

Try:

=IF(ROUND(C1,1)=0.1,"yes","no")
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,728
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top