简体   繁体   English

ASP.NETCore model 中的十进制字段格式

[英]Decimal field format in ASP.NETCore model

In SQL Server I have a primary key of a Table declared as follow:在 SQL 服务器中,我有一个表的主键,声明如下:

[CustomerID] [numeric](18, 0) IDENTITY(1000000,1) NOT NULL,

In ASP.NET Core 2.2 model i mapped this field ad follow:在 ASP.NET 核心 2.2 model 中,我映射了这个字段广告如下:

public decimal CustomerId { get; set; }

How can I make the decimal digits not appear in the application?如何使小数位不出现在应用程序中?

Use DisplayFormat attribute:使用 DisplayFormat 属性:

[DisplayFormat(DataFormatString = "{0:0}", ApplyFormatInEditMode =true)]
public decimal CustomerId { get; set; }

If you only need to display it without decimal places, then the formatting can be applied with a ToString anywhere, preferably where you are trying to display it.如果您只需要显示它而不显示小数位,则可以在任何地方使用ToString应用格式,最好是在您尝试显示它的位置。 The code below will apply rounding.下面的代码将应用舍入。

CustomerId.ToString("0");

If you need to use it in some other capacity without the decimal points then you should map your model representing the database table to another model specific to what you are trying to do and supply the correct type in that model. If you need to use it in some other capacity without the decimal points then you should map your model representing the database table to another model specific to what you are trying to do and supply the correct type in that model. Probably an int or string in this case.在这种情况下可能是intstring

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM