Link to home
Start Free TrialLog in
Avatar of Wanting2LearnMan
Wanting2LearnManFlag for United Kingdom of Great Britain and Northern Ireland

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++:

int originalNumber = 100000;
double reduceBy = (100 - m_percent) / 100;
int reducedNumber = (int)(originalNumber * reduceBy );

Open in new window


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
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Please give some more example of what you mean reduce by.

My interpretation would be

reducedNumber = (100-m_percent) * originalNumber;
Avatar of Wanting2LearnMan

ASKER

Thanks jkr, it now works.

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.