[英]Display decimal without trailing zeros in MVC view
How to display decimal with 3 zeros after comma, without trailing? 如何在逗号后显示带三个零的十进制而不尾随? My code is 我的代码是
<td>@decimal.Parse(item.QuantityKg.ToString()).ToString("G29")</td>
Since your QuantityKg
is decimal?
由于您的QuantityKg
是decimal?
instead of decimal
, you can use it's Value
property and use "N"
format specifier with 3
precision. 代替decimal
,可以使用它的Value
属性,并使用3
精度的"N"
格式说明符 。
If everything is okey other than this, this should work in your case; 如果除此以外一切还不错 ,这应该适合您的情况;
<td>@item.QuantityKg.Value.ToString("N3")</td>
But this throws InvalidOperationException
if QuantityKg
is null
, so using string.Format
would be better to catch this situation which generates empty string if it is null
as Panagiotis mentioned . 但是,如果QuantityKg
为null
,则会抛出InvalidOperationException
,因此使用string.Format
会更好地解决这种情况,如果Panagiotis 提到它为null
,则会生成空字符串。
<td>@string.Format("{0:N3}", item.QuantityKg)</td>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.