user2005657
user2005657

Reputation: 627

linq sum how to display as currency

I have the following in a razor view:

<td>@payments.Sum(p => p.Amount)</td>

I want it to display as a currency so have '$' and two decimals on the end.

I think normally you go something like {0:C}.

I don't know how to incorporate this into what I've got as the Sum does not have an overload for format.

Do I need to do this with a css class?

Upvotes: 2

Views: 1923

Answers (2)

David Hoerster
David Hoerster

Reputation: 28711

<td>@payments.Sum(p => p.Amount).ToString("C")</td>

I think this is a slightly more efficient call than String.Format. See this answer.

Upvotes: 4

Yuck
Yuck

Reputation: 50865

You would do this:

<td>@String.Format("{0:C}", payments.Sum(p => p.Amount))</td>

Upvotes: 5

Related Questions