【问题标题】:Format attribute to display 8 decimal places in ASP.net core在 ASP.net 核心中显示 8 位小数的格式属性
【发布时间】:2020-07-10 07:45:25
【问题描述】:
我想将我的 latitude 和 longitude 显示到小数点后 8 位。但是,我现在默认只显示到小数点后 2 位。我应该如何更改我的模型?
型号:
public class LocationModel
{
[Display(Name = "Latitude")]
public decimal Latitude { get; set; }
[Display(Name = "Longitude")]
public decimal Longitude { get; set; }
}
【问题讨论】:
标签:
c#
asp.net
asp.net-core
model
decimal
【解决方案1】:
两种选择:
- 数据格式字符串
public class LocationModel
{
[Display(Name = "Latitude")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:G8}")]
public decimal Latitude { get; set; }
[Display(Name = "Longitude")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:G8}")]
public decimal Longitude { get; set; }
}
- 数学
public class LocationModel
{
private decimal _latitude;
private decimal _longitude;
[Display(Name = "Latitude")]
public decimal Latitude
{
get
{
return Math.Round(_latitude, 8);
}
set
{
this._latitude = value;
}
}
[Display(Name = "Longitude")]
public decimal Longitude
{
get
{
return Math.Round(_longitude, 8);
}
set
{
this._longitude = value;
}
}
}