【发布时间】:2015-07-03 07:49:00
【问题描述】:
我有一个 ViewModel 需要从多个双数据类型属性返回多个值。我尝试使用 DisplayFormat 数据注释来确保双精度数返回到小数点后两位。
这不起作用,因为有几个字段返回大量小数。如何确保我的 Web API 视图模型只返回两位小数?
【问题讨论】:
标签: c# asp.net-web-api data-annotations
我有一个 ViewModel 需要从多个双数据类型属性返回多个值。我尝试使用 DisplayFormat 数据注释来确保双精度数返回到小数点后两位。
这不起作用,因为有几个字段返回大量小数。如何确保我的 Web API 视图模型只返回两位小数?
【问题讨论】:
标签: c# asp.net-web-api data-annotations
这应该可以解决...但是,如果您想始终显示 2 位小数,则必须调用此方法 .ToString("#.00")
public class ViewModel
{
private double randomNumber;
public double RandomNumber
{
get
{
return Math.Round(randomNumber, 2);
}
set
{
randomNumber = value;
}
}
}
【讨论】: