REMESQ
REMESQ

Reputation: 1200

Format decimal to currency in @ViewBag

I am calling a decimal thusly in my view:

@ViewBag.CalculatePrice

How can I format that to represent $xxx.xx?

CalculatePrice is just a public decimal CalculatePrice() { //performing calculation } in a class.

Thanks.

Upvotes: 3

Views: 7528

Answers (2)

Justin Morgan
Justin Morgan

Reputation: 30760

You can explicitly cast it to decimal; then you'll be able to treat it as though it were statically-typed.

@(((decimal)ViewBag.CalculatePrice).ToString("C2"))

If you were wondering how to specify formatting for a decimal, here's a reference for .NET's standard numeric formatting strings.

Upvotes: 8

Dave Lawrence
Dave Lawrence

Reputation: 3929

Surely you just need to use Use String.Format. I'm confused because this seems a little too obvious to me.

Are you assigning ViewBag.CalculatePrice in your controller?

Upvotes: 1

Related Questions