Reputation: 11911
I am trying to divide two items like so:
<td>@item.Sum(m => m.stat.hits) / @item.Sum(m => m.stat.AB)</td>
but it just results this in text format like this
1197 / 3956
What am I doing wrong?
I am expecting the outcome .303
UPDATE
got it working by
<td>@Math.Round(Decimal.Divide(item.Sum(m => m.stat.hits), item.Sum(m => m.stat.AB)), 3)</td>
which returns 0.303
How do I remove the zero before the decimal?
Upvotes: 1
Views: 2018
Reputation: 878
This is easy way:
static void Main() {
String str ="0.2354".TrimStart(new Char[] { '0','.' } );
Console.WriteLine(str);
}
For number 0.0000002354
it works correctly.
Upvotes: 0
Reputation: 26
Inside the curly brackets, you can do programming at razor view. Here is the sample code those you need to do for your solution.
<td>
@{
double sumHits = item.Sum(m => m.stat.hits);
double sumAb = item.Sum(m => m.stat.AB);
double result = sumHits / sumAb;
if (sumAb > 0)
{
double result = sumHits / sumAb;
<Span>@result</Span>
}
}
</td>
Upvotes: 0
Reputation: 243
Pre-face this by saying I'm on my mobile device so please excuse any typos.
You need to encompass the operation with braces, like so:
<td>@{ item.Sum(m => m.stat.hits) / item.Sum(m => m.stat.AB) }</td>
However the Sum()
operation returns an integer so you may need to convert it into a double
to display correctly.
That being said while this solution should give you the correct answer I would highly recommend simply adding the operation as a property on the object and accessing it instead of calculating it on the fly.
EDIT:
Couple of ways to remove leading zeroes. Casting to a string and calling str.TrimStart('0')
Upvotes: 2