c# - 格式化包含公式的 get/return
问题描述
我对如何格式化下面的 get/return 以使小数点后只有 2 位数字感到有些困惑,非常感谢任何帮助。
public decimal? aExample { get; set; }
public decimal? bExample { get; set; }
public decimal? Combined
{
get { return aExample / bExample; }
}
[Display(Name = @"ABC %")]
public decimal? Combined => aExample.NullableDivide(bExample);
解决方案
您可以使用Math.Round()
功能,
//Important: This will loose precision of result. It will round value to its closed value
public decimal? Combined
{
get {
return aExample == null || bExample == null ?
null
: Math.Round((aExample / bExample) ?? default(decimal), 2); //Return result with 2 precision.
}
}
在线尝试:.NET FIDDLE
推荐阅读
- node.js - 如何在 nodejs 的 microsoft bot 模拟器中显示加载选项?
- angular - 如何获取数组响应以保存到文本文件
- c# - 使用Epplus的Excel时间格式
- javascript - 与 streetViewService 异步 - 回调使用失败
- android - 在 gradle 窗口中,它只是说“没什么可显示的”
- java - @Inject 和 @ValueMapValue 注解的区别
- powershell - 将控制台输出写入文件 - 文件意外为空
- jquery - Amazon S3 文件上传:抛出 403
- azure - 如何将密钥保管库的访问权限授予用户分配的身份?
- android - 使用 SnapHelper 选择项目时在 Viewholder 中调用函数