29

I have following POCO with Cost as float.

public class CostChart
{
    public string itemType { get; set; }
    public float? Cost{ get; set; }

}

I need to return Cost in currency format e.g.

$U 4.882,50.

What data annotation should I use?

This is how I am displaying in my view.

<td>@Model.Cost</td>
Rick Rainey
  • 11,096
  • 4
  • 30
  • 48
TheKingPinMirza
  • 7,924
  • 6
  • 51
  • 81

6 Answers6

65

Have you tried using DataType.Currency:

public class CostChart
{
    public string itemType { get; set; }
    [DataType(DataType.Currency)]
    public float? Cost{ get; set; }

}

Alternatively, you could use DataFormatString like this:

[DisplayFormat(DataFormatString = "{0:C0}")]`
public float? Cost{ get; set; }

But I prefer to set the display format with EditorFor. Here's a great tutorial on Extending Editor Templates for ASP.NET MVC tutorial.

That way, you write the display logic of your currencies in just ONE place, and you don't need to add that extract annotation every single time you want to display a currency amount.

--Edit

To make it work in EditorFor, you can also add ApplyFormatInEditMode = true to the end of the DataFormatString making the line as:

[DisplayFormat(DataFormatString = "{0:C0}", ApplyFormatInEditMode = true)]
Raj Baral
  • 661
  • 6
  • 19
Cacho Santa
  • 6,846
  • 6
  • 41
  • 73
  • I tried above but it is not changing my value to currency on my view, it is still showing value without currency format. I am using this line on my view td>@Model.Cost – TheKingPinMirza Apr 30 '15 at 18:46
  • Try the editorfor template version in the tutorial. In that case, it should be @Html.EditorFor(m => m.Cost). – Cacho Santa Apr 30 '15 at 18:48
  • @HTML.EditorFor() just convert label into editable field with value showing 1233.44 and not in currency format. Am i missing some point here> – TheKingPinMirza Apr 30 '15 at 18:54
  • Sorry, it should have been @Html.DisplayFor( ), like in the tutorial. – Cacho Santa Apr 30 '15 at 18:55
  • 4
    decimal is a better datatype – niico Jan 28 '20 at 15:32
  • thanks @niico, I agree. I'd definitely go with decimal data type for this. – Cacho Santa Jan 28 '20 at 18:32
  • I have a similar issue, not with displaying the number, although DataType(DataType.Currency) does nothing for me (MVC 5 Framework 4.62) I can display the number using string formatting. The issue is that when posting back, the validation says that this is not a number, and the value is either 0 or null on the controller. I have seen the use of binders and JavaScript code, but seeing this as a common use, I can't believe we have to go to such lengths to have a monetary value posted back correctly. – Shiin Zu Jan 09 '23 at 17:49
11

Try using:

 [DisplayFormat(DataFormatString = "{0:C0}")]

Visit this post https://stackoverflow.com/a/19800496/3642086

Community
  • 1
  • 1
Márcio Gonzalez
  • 1,020
  • 1
  • 8
  • 20
7

try this :

public class CostChart
  {
    public string itemType { get; set; }

    [DataType(DataType.Currency)]
    public float? Cost{ get; set; }
  }

if you still do not see $ symbol, in web.config under write this line of code

<globalization culture ="en-us"/>
Rachit Thakur
  • 71
  • 1
  • 1
3

All right, when we talking about data annotations, there's two things to take in account:

The Data Annotation, which is:

[DisplayFormat(DataFormatString = "{0:C0}")]
public decimal? Cost{ get; set; }

And the tag in Razor pages, which is:

@Html.DisplayFor(model => model.Cost)

If you don't use "DisplayFor", data annotations is useless.

Another detail, if you want the decimal place, you should remove the last "0" after "C", as follow:

[DisplayFormat(DataFormatString = "{0:C}")]

Hope it helps you and good luck!

0

You can do in RazorView

@string.Format("{0:0,0.00,00}", Model.Cost)
venogn
  • 56
  • 1
  • 6
0

Try something like this if you want to show 4 digit decimal points (the number after C is showing the point number)

  • This works for float/decimal and double types:

    [Display(Name="Price")] [DisplayFormat(DataFormatString = "{0:C4}")] public double Price { get; set; }

InkHeart
  • 810
  • 1
  • 7
  • 17