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.

4

2 に答える 2

3

メソッドを使用してビュー自体Formattedに値を渡すことで、これを行うことができました。{0:N}

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

hereからフォーマット文字列を取得し、ここFormattedで使用されているメソッドを見つけることで答えを見つけました

{0:C}値を通貨として表示しますが、通貨記号は表示しないので、これは私にとって完璧です

于 2018-10-18T12:20:02.777 に答える