Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

code does work when changed to amount/budget 1

Status
Not open for further replies.

HappyAdam

Programmer
May 6, 2010
6
US
the following code is producing the wrong percent. THis is simple spending/bud to give the percent spent of the budget and the percent availalbe. But when i code this change the app blows up.
as you can see here but when i flip budge.value and i.amount, it blows up.




decimal budgetSum = StateManager
.Instance.GetCategoryBudget(c);
i.BudgetDiffExpense = budgetSum - i.Amount;



// (from t in transactions






// select t.Budget).FirstOrDefault();



i.Budget =


Math
.Abs(i.Amount)-i.BudgetDiffExpense.Value;




//if (i.BudgetDiffExpense.Value < 0)






// i.Variance = 100 - i.BudgetDiffExpense.Value / i.Amount * 100 * -1;






//else






if
(i.Amount != 0)
{

i.Variance = (i.Budget.Value / i.Amount - 1);

}





else



{

i.Variance =


null
;
}





if
(i.Budget != 0)
i.PercDiff = i.Amount / i.Budget ;





Logger.Debug("SpendingListCtrl.cs: CreateCategoryChartLegendItem: budget: {0}, Amount: {1}"
,
i.Budget, i.Amount);





return
i;
}
 
if you are just getting the wrong percent then your math is wrong. try separating the query from the calculation and write some automated tests to validate your code.

if your code is "blowing up" then you need to provide more details. The exception and stack trace would be a good place to start.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
It looks like there is a null that is causing the problem.
as there are some budgets that are 0.

How might I code to prevent this?
 
check for null

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top