ProgrammingFreak
ProgrammingFreak

Reputation: 47

C#: Calculate Percentage of new items added over a course of time?

Say I have a database to which new rows are added everyday. Everyday a percentage of new data added to the database should be calculated against what was there before. That is; show the user a percentage of new data added to the database accumulated within a week.

What I have so far:

For example, if I have:

int oldCount;
int newCount;

How do I calculate the percentage of growth from the oldCount to the newCount so that it displays:

for example:

10% growth detected

Upvotes: 0

Views: 154

Answers (1)

Dmitry
Dmitry

Reputation: 14059

Assuming that both counts are non-negative, try to use this:

double percent = oldCount == 0 ? 100 : ((double)newCount - oldCount) / oldCount * 100;

Upvotes: 2

Related Questions