Wanting2LearnMan
asked on
How do I simply reduce a number by a ceratin percent in C++.
I want to reduce a number by a certain percentage so I have the following calculation in C++:
If m_percent is e.g. 10 then reduceBy will be 0.000000000000.
How do I simply reduce a number by a ceratin percent in C++.
int originalNumber = 100000;
double reduceBy = (100 - m_percent) / 100;
int reducedNumber = (int)(originalNumber * reduceBy );
If m_percent is e.g. 10 then reduceBy will be 0.000000000000.
How do I simply reduce a number by a ceratin percent in C++.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks jkr, it now works.
Is the above code fine? or is there a 'better' way to do this calcluation???
Is the above code fine? or is there a 'better' way to do this calcluation???
The code seems OK. The problem was just that if integers are in the game, you can easily truncate values to zero by a single multiplication, thus the caveat about floating point operations.
My interpretation would be
reducedNumber = (100-m_percent) * originalNumber;