4

I need to display some average sums of currency values in my .net core asp mvc project.

I am using mvc 6 grid to display the data, and I have tried these solutions:

[DisplayFormat(DataFormatString = "{0:C}")]
public double? AverageCost { get; set; }

[DisplayFormat(DataFormatString = "{0:#.####}")]
public double? AverageCost { get; set; }

[RegularExpression(@"^\d+\.\d{0,2}$")]
public double? AverageCost { get; set; }

But still my values are displayed with several decimal places:

enter image description here

Am I missing something?

I know I can format the columns using the mvc 6 grid, but is there not a way of doing this in the ViewModel?

Note that this is NOT a duplicate of the suggested question.. If you actually read the questions you will see that.

Bassie
  • 9,529
  • 8
  • 68
  • 159
  • 4
    `[DisplayFormat(DataFormatString = "{0:C2}")]` for 2 digits https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings#the-currency-c-format-specifier – fubo Oct 18 '18 at 11:38
  • 2
    _"sums of currency values"_ & _"**double**? AverageCost"_ = bad idea. – Fildor Oct 18 '18 at 11:38
  • @Fildor Yes I know, but I don't control the database, and the currency values are stored as double?s in there.. – Bassie Oct 18 '18 at 11:39
  • Ah, that's too bad. Anyway, fubo's suggestion should do the job. – Fildor Oct 18 '18 at 11:40
  • @fubo It doesn't seem to work - I guess the grid I'm using disregards `DisplayFormat` – Bassie Oct 18 '18 at 11:41
  • 1
    `[DisplayFormat]` is only respected when you use `DisplayFor()` (or `EditorFor()`) –  Oct 18 '18 at 11:43
  • Maybe this will help: http://mvc6-grid.azurewebsites.net/Column/RenderingValue? – Fildor Oct 18 '18 at 11:44
  • @Fildor The closest I can get is using `{0:C}`, but that includes the currency symbol, which I don't want – Bassie Oct 18 '18 at 11:48
  • Have you tried `{0:D2}` then? – Fildor Oct 18 '18 at 11:49
  • @Fildor I found the answer - its to use `{0:N}`, but some fool marked this as a dupe so I cant post the answer.. how irritating – Bassie Oct 18 '18 at 11:50
  • Haha, "some fool" ... well, it _is_ a duplicate regarding the DisplayFormat annotation. It just has an additional issue about the format string, which makes it "2 Question in 1" ... – Fildor Oct 18 '18 at 11:51
  • @Fildor But this question relates to mvc-6 grid, which has different requirements so it absolutely is not a dupe. The solutionn posted as a duplicate will not work here... – Bassie Oct 18 '18 at 11:52
  • Did you try it? If you did and it did not work for you, I'll vote to reopen. – Fildor Oct 18 '18 at 11:54
  • @Fildor Well its hard because the posted solution is actually not even relevant... its about dates, and it also relates to using `DisplayFor`, which my question has nothing to do with! The solution is to use `Formatted()` method in the view – Bassie Oct 18 '18 at 11:55
  • On second reading it really doesn't seem to be related to your issue exactly. Voted to reopen. – Fildor Oct 18 '18 at 11:56
  • 1
    @Fildor Thanks buddy, hopefully it get re-opened so I can reap some sweet sweet SO point – Bassie Oct 18 '18 at 11:59

2 Answers2

3

I was able to do this by using the Formatted method and passing in the value {0:N} in the view itself:

columns.Add(model => model.AverageCost).Titled("AverageCost").Formatted("{0:N}");

I found the answer by getting the format string from here and finding the Formatted method used here

This is perfect for me as it displays the values as currency but does not show the currency symbol as {0:C} does

Bassie
  • 9,529
  • 8
  • 68
  • 159
2

In your first line you missed the decimal points after 'C'. it should have been [DisplayFormat(DataFormatString = "{0:C2}")]

Hamed
  • 356
  • 2
  • 9