user2232273
user2232273

Reputation: 4964

Doing Math Operation using MVC Razor

what i try to do is a simple math operation in my project but something works wrong because i get always 0. So my question is:

how can i do this math operations using mvc razor?

what i try is this:

@{
  decimal a = 2 / 4;  //--> result 0,5
  int b = a * 100   //--> 50
}

the problem is that the first result of the variable a give me 0,5 and i need this value in decimal, but then i wanted to multiplicate it with 100 that gives me the 50 of datatype int.

hmm, but i can´t figured it out how to do that..

can someone give me a hand with this pls??

Upvotes: 1

Views: 16068

Answers (2)

ozidom
ozidom

Reputation: 159

The following works:

decimal a = 2m / 4m;  //--> result 0,5
int b = (int)(a * 100m);

Upvotes: 1

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

You are performing Integer division which gives you Zero here yYou need to convert one of the value into decimal while performing division.

Try This:

decimal a = 2.0M / 4;
int b = Convert.ToInt32(a * 100);

Upvotes: 4

Related Questions