使用MVC6网格时无法将显示格式设置为小数点后2位 [英] Can't set display format to 2 decimal places when using mvc6 grid
问题描述
我需要在.net core asp mvc项目中显示一些平均货币价值总和.
我正在使用
我想念什么吗?
我知道我可以使用mvc 6网格设置列的格式,但是在ViewModel中没有做到这一点的方法吗?
请注意,这与建议的问题不重复.如果您实际阅读这些问题,将会看到该问题.
我能够通过使用 Formatted
方法并传入值 {0:N} 视图本身中的code>:
columns.Add(model => model.AverageCost).Titled("AverageCost").Formatted("{0:N}");
我找到了答案,方法是从此处获取格式字符串,然后找到 Formatted
方法使用的方法是此处
这对我来说是完美的,因为它将值显示为货币,但不显示货币符号,如 {0:C}
一样
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:
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.
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
这篇关于使用MVC6网格时无法将显示格式设置为小数点后2位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!