An easy way is to find out how large the sensor is, such as its dimensions (number of vertical pixels and number of horizontal pixels). Then you multiply the two together.
Also, the "mega" can be decimal as you've assumed (where you assume 3 megapixels is 3000000 pixels), but that's not always the case:
A megapixel (MP) is a million pixels; the term is used not only for the number of pixels in an image, but also to express the number of image sensor elements of digital cameras or the number of display elements of digital displays. For example, a camera that makes a 2048×1536 pixel image (3,145,728 finished image pixels) typically uses a few extra rows and columns of sensor elements and is commonly said to have "3.2 megapixels" or "3.4 megapixels", depending on whether the number reported is the "effective" or the "total" pixel count.
So I think the problem comes from the assumption that 3 megapixels should be exactly 3,000,000, when in reality it could be slightly different. See above where you have an exact number of pixels, but could call it two different megapixel names (3.2 or 3.4) yet nowhere do the literal numbers 3,200,000 or 3,400,000 come up exactly, it's approximated. So similarly, you probably can't perform your calculation using exactly 3,000,000, and that causes the math error.
This issue arises because marketing has to make things very simple for buyers. Giving exact pixel count can complicate it too much for typical shopper, so you dumb it down and just say megapixel.