Reputation: 5859
I have a web service that returns a decimal value into my json string but it rounds it off on numbers that are 400.00 to 400 instead, but if it has 400.50 it returns 400.50 which is what i want how do i return a value that is 400 to 400.00 in that format
Error
{"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.","
Code
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();
Upvotes: 0
Views: 256
Reputation: 6451
Whether you return 400, or 400.00 should not matter for anything mathematical so I'm assuming that this is regarding displaying a decimal to a user. In this case you can supply a string format to decimal's ToString().
decimal number = 400m;
Console.WriteLine(number.ToString("N2")); // Outputs 400.00
This will always return up to two decimal places. Please note however, that it is no longer a number at this point, but a string. If you are using the webservices' JSON for both display and math then I suggest returning the decimal, and formatting the string client side through javscript number.toFixed(2);
Upvotes: 3