gpt4 book ai didi

c# - Razor 中的 DisplayFor 具有可为空的十进制更改格式

转载 作者:太空宇宙 更新时间:2023-11-03 10:28:40 25 4
gpt4 key购买 nike

我的模型有一个属性,例如:

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

在我的 Razor View 中,我有:

<span class="large bold">- @Html.DisplayFor(x => x.Rate)</span>

问题是这样的:我需要显示的数字是 28.71(十进制?)如果 DisplayFor 使用 x => x.Rate 则 View 显示 2.871 但如果 DisplayFor 使用x => x.Rate.Value 然后 View 显示正确的数字 28.71

这里发生了什么??

最佳答案

来自 MSDN :

The percent ("P") format specifier multiplies a number by 100 and converts it to a string that represents a percentage. The precision specifier indicates the desired number of decimal places.

{0:P0} 格式会将您的十进制数 (28.71) 显示为 28.71 * 100 = 2,871 %(假设您使用的是 en-US 文化).

如果你只是想在数字上加上百分号并显示它,使用这个:

[DisplayFormat(DataFormatString = "{0} %")]

关于c# - Razor 中的 DisplayFor 具有可为空的十进制更改格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30942500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com