1

I've created a application in C# with SQL Server 2008 R2 backend. In my DB one of the table contains a column with datatype money. I'm using Entity Framework (Database First) to access data from the DB. In my EF Model, the money type attributed is represented as decimal.

Now when I serialize an object of that entity, that decimal property is represented as having 4 decimal places. I need this to have only 2 decimal places. I'm using JSON.NET to serialize my objects into JSON.

Here is what I'm getting (see the value of Price property)

{
  ID: 1,
  Name: "Product1"
  Category: 
    {
      ID: 1,
      Name: "Category1"
    },
  Price: 200.0000
}

Here is what I'm need (see the value of Price property)

{
  ID: 1,
  Name: "Product1"
  Category: 
    {
      ID: 1,
      Name: "Category1"
    },
  Price: 200.00
}

Is there any settings that I need to provide while serialization? Please give me any hint.

Krishanu Dey
  • 6,326
  • 7
  • 51
  • 69
  • There are several points in the code at which you could do the rounding depending on how your retrieving your data before pushing it to the client. Can you provide a code sample of where you do this? – Dillie-O May 07 '14 at 16:14
  • 1
    see http://stackoverflow.com/questions/12283070/serializing-a-decimal-to-json-how-to-round-off – Raphaël Althaus May 07 '14 at 16:17
  • @RaphaëlAlthaus :: Oops.. Actually I didn't find this post, Anyway, thanks for the reference link. – Krishanu Dey May 07 '14 at 16:21

1 Answers1

0

If in your db you see price being stored as

Price: 200.0000

then you can change your model get property to return only to 2 decimal place or if you can force database to store only 2 decimal places

decimal Price {get;set;}

you need to implement the get function to round to 2 decimal place

Anshul Nigam
  • 1,608
  • 1
  • 12
  • 26