1

I need to display a euro value from a decimal value that is stored in a SQL-Server database.

The value comes from the database like this: 10000,0000 and I need to display it like this: 10.000,00 €

How I can do this?

I tried formatting the ViewModel:

 [DisplayFormat(DataFormatString = "{0:c}")]
 public decimal Price { get; set; }

And display it like this:

@Html.DisplayFor(modelItem => item.Price)

But I get ¤10,000.00

Any suggestions?

Thanks.

stderr
  • 8,567
  • 1
  • 34
  • 50
Patrick
  • 2,995
  • 14
  • 64
  • 125

1 Answers1

4
[DisplayFormat(DataFormatString = "{0:n} €")]

"{0:c}" is using the current culture information (currency) to format the value.

Peter Kiss
  • 9,309
  • 2
  • 23
  • 38
  • 2
    Wouldn't it be better with `€` instead? – cheesemacfly May 20 '13 at 01:17
  • That is the HTML representation of the Euro sign but the model can be displayed for example in a desktop application too so i think the HTML representation would be create a hard dependency on HTML display. – Peter Kiss May 20 '13 at 01:19
  • But since the question is tagged with asp.net-mvc, it would be safe to assume the display to always be HTML, no? – cheesemacfly May 20 '13 at 01:21
  • Hi, thanks but it's formatting like this: 10,000.00 € and I need the format 10.000,00 € – Patrick May 20 '13 at 08:53
  • 1
    Yes becouse the N format is also using the current culture. Check http://msdn.microsoft.com/en-us/library/0c899ak8.aspx for custom number formats. – Peter Kiss May 20 '13 at 08:57
  • Using @string.Format("{0:n} €", item.Price) in the View is working fine, so how can I pass the format from the ViewModel to the View regarding the culture? thanks – Patrick May 20 '13 at 09:46
  • Create an EditorTemplate (http://stackoverflow.com/questions/5300525/how-do-i-render-only-some-specific-model-fields-with-an-invariantculture-while) or create a custom Html helper (inside that you can use string.Format with invarian culture), i recommend the second one. – Peter Kiss May 20 '13 at 09:52
  • To simplify I have converted the Price to an Integer, so now I need to convert 10000 € to 10.000 €, any idea? – Patrick May 20 '13 at 11:08