c# - 在 ASP.net 核心中显示 8 位小数的格式属性
问题描述
我想显示我的latitude
andlongitude
到小数点后 8 位。但是,我现在默认只显示到小数点后 2 位。我应该如何更改我的模型?
模型:
public class LocationModel
{
[Display(Name = "Latitude")]
public decimal Latitude { get; set; }
[Display(Name = "Longitude")]
public decimal Longitude { get; set; }
}
解决方案
两种选择:
- 数据格式字符串
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;
}
}
}
推荐阅读
- google-sheets - 谷歌表格,如何创建一个 Vlookup 表并获取每个客户的最后一次出现
- javascript - 我可以在不同的页面中编写验证函数并在我的猫鼬模式中调用它吗
- python - 为什么在这里提出 django TemplateSyntaxError ?
- swift - 在 swift 代码中导入 TensorFlowLite(没有这样的模块错误)
- typescript - Lint staged + ESLint 正在测试所有文件而不是 staged
- reactjs - 在反应中导航到我页面的不同部分
- php - 简码中的 WooCommerce do_action 弄乱了页面布局
- python-3.x - 如何使用 DataFrame 将 csv 文件转换为嵌套的 json 格式
- haskell - 和有什么区别。和 (.) 在 Haskell 中?
- r - 贬低一个数据集,我发现了两种导致不同结果的不同方式