0

C#

homeModel.FinancialComplianceModelList.AnnualDebtService=20;
homeModel.FinancialComplianceModelList.AnnualLeaseExpense=50;
homeModel.FinancialComplianceModelList.PPR=100;


decimal DebtAndLeaseToPPRRatio = Convert.ToDecimal((homeModel.FinancialComplianceModelList.AnnualDebtService + homeModel.FinancialComplianceModelList.AnnualLeaseExpense) / homeModel.FinancialComplianceModelList.PPR);

I Calculated the values for like above formula . The calculated values is

0.35 

I want a digit values. So i use math.round()

Int finalvalue=Convert.ToInt32( Math.Round(DebtAndLeaseToPPRRatio));

Now That FinalValue is 0 //It's good

Javascript

Now I used the same formula with same values in a javascript for see this below code

 $("#DebtAndLeaseToPPRRatio").text(Math.round(((AnnualDebtService + AnnualLeaseExpense) / PPR).toFixed(1)) + "%");

But Now it's display result is 1 .

When i used the same formula Why the c# is display 0? and Javascript is return 1 ?

  • Check you variables in javascript, as if you do just Math.round(0.35) in javascript it will give you 0 – vittore May 25 '13 at 04:58

2 Answers2

1

UPDATE: Generally speaking Math.round in c# and javascript is doing the same thing. However there are several nuances. For .net details go to Jon Skeet answer here, for some of javascript issues you can find reference on MDN and this SO answer.

Before you go any further I would suggest you read carefully about three operations on float point numbers round, floor and ceiling.

Here is starting poing for .net

http://msdn.microsoft.com/en-us/library/system.math.round.aspx

And one for javascript

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Math/round

If you do simple test of Math.round(x) for the following numbers 0.1 0.5 .07 0.9, you will get the same result in both javascript and .net

Take into account that in some browsers javascript can give you "funky" results with big numbers

Check this answer Using Math.round in javascript adds weird number of 0s at the end

Also, just for purpose of learning, read about bankers rounding here Why does Math.Round(2.5) return 2 instead of 3?

Community
  • 1
  • 1
vittore
  • 17,449
  • 6
  • 44
  • 82
-1

I think the problem is elsewhere:

If we go with the values that you provided in your question:

homeModel.FinancialComplianceModelList.AnnualDebtService=20;
homeModel.FinancialComplianceModelList.AnnualLeaseExpense=50;
homeModel.FinancialComplianceModelList.PPR=100;

decimal DebtAndLeaseToPPRRatio =
    Convert.ToDecimal((homeModel.FinancialComplianceModelList.AnnualDebtService +
                       homeModel.FinancialComplianceModelList.AnnualLeaseExpense)/
                       homeModel.FinancialComplianceModelList.PPR);

Assuming that at least on of the variables involved in the calculation is a decimal DebtAndLeaseToPPRRatio is now 0.7 (otherwise if you're calculating ints you'd get 0). Because (20.0 + 50.0) / 100 = 0.7, however note that (20 + 50) / 100 = 0.

Not sure where you're getting 0.35. Also Math.Round(0.35) (C#) and Math.round(0.35) (JavaScript) would get you 0. The difference is when you do Math.Round(0.5) (C#) -> 0.0 where Math.round(0.5) (JavaScript) -> 1.

I'm testing your code in both languages (with the values provided) and I'm getting 1 in each case.

Dimitar Dimitrov
  • 14,868
  • 8
  • 51
  • 79
  • Where i use the Math.floor() /in c# or In javascript ? –  May 25 '13 at 04:56
  • 2
    That is obviously wrong answer. floor and round two completely different operations. – vittore May 25 '13 at 04:57
  • @DimitarDimitrov check this three links http://msdn.microsoft.com/en-us/library/system.math.round.aspx http://msdn.microsoft.com/en-us/library/system.math.floor.aspx http://msdn.microsoft.com/en-us/library/system.math.ceiling.aspx – vittore May 25 '13 at 05:03
  • 1
    @DimitarDimitrov Obvious difference would be for value 0.7 for instance. `round` would round it to 1 and that is what you are expecting from round. floor would make it 0. and topic starter will file another question here, why 0.7 is not being rounded to 1. – vittore May 25 '13 at 05:13
  • @vittore yes, you're right, however if he's asking how to round down, math.floor() would be correct. Anyway, I think the problem is in his calculation (you can see my edit), I just didn't notice it. – Dimitar Dimitrov May 25 '13 at 05:53