Floating: Rounding to a place

Ok, let me start off by saying that this is not homework, and that I'm learning C++ on my own. I would appreciate your help.

Here is my problem:

I am creating a budget program (works great so far!) but there is a tiny problem:

Calculating gross, by subtracting (float)money_spent from (float)Money_allocated will yield a value greater than zero even if the two numbers are exactly the same (the number is really small though). The problem with this is that I wrote in an algorithm that takes that gross and determines it's relation to your budget: ie. Do you make your budget or not.

So, when the Gross is greater than 0, you made you budget. If gross is less than zero, you have over spend, and if gross equals zero (which currently is being a pain in the ass right now) it advises that you put more into your budget.

Now, I have read that floats behave oddly when it comes to rounding, and I assume that is the reason that I can not get an absolute zero from a mathmatical equation involving these two floats. However, the difference is so small, that rounding to the hundreth's place (which i would like to have done to begin with) would achieve my goal. I have tried integer extraction, but that didn't work either, it just gave me a smaller number. So, I would like to know if there is a fuction in C++ which will allow me to specify how many places a float should/will round to. Thank you for your time!
Last edited on
NVM! I figured it out. I used the following equation:

gross = ((floor(gross * 100)) / 100)
Topic archived. No new replies allowed.