webservice返回舍入的数字而不是小数

时间:2011-10-30 23:49:01

标签: jquery asp.net

我有一个Web服务,它将一个十进制值返回到我的json字符串中,但是它会将其四舍五入为400.00到400的数字,但是如果它有400.50则返回400.50这就是我想要的如何返回一个值那种格式是400到400.00

错误

{"Message":"LINQ to Entities does not recognize the method \u0027System.String ToString(System.String)\u0027 method, and this method cannot be translated into a store expression.","

代码

    Products = (
    from p in db.Products where p.DepartmentId == qDepartment.Id
    join i in db.ProductImages on p.Id equals i.ProductId into products
    from x in products.Where(y => y.Dimension == "180X180")
    select new Product
    {
        Id = p.Id,
        Title = p.Title,
        ShortDescription = p.ShortDescription,
        Brand = p.Brand,
        Model = p.Model,
        Image = x.Path,
        FriendlyUrl = p.FriendlyUrl,
        SellPrice = p.SellPrice.ToString("N2")/*Error Here changed type to string*/,
        DiscountPercentage = p.DiscountPercentage,
        Votes = p.Votes,
        TotalRating = p.TotalRating
    }).ToList();

1 个答案:

答案 0 :(得分:3)

无论你返回400还是400.00对于任何数学都无关紧要,所以我假设这是关于向用户显示小数。在这种情况下,您可以为小数的ToString()提供string format

decimal number = 400m;
Console.WriteLine(number.ToString("N2")); // Outputs 400.00

这将始终返回最多两个小数位。但请注意,此时它不再是数字,而是一个字符串。如果您使用webservices的JSON进行显示和数学运算,那么我建议返回小数,并通过javscript格式化字符串客户端number.toFixed(2);