How do I control the default string formatting that Entity Framework applies to decimal values? RRS feed

  • Question

  • I have a database-first entity with a nullable decimal property (in the backing store it is a NUMBER(14,8)).  When I ToString this decimal I am getting 2 decimal places without specifying any formatting at all.  If I take a decimal? that is not an entity property and ToString it I get the value back without a fixed set of decimal places.

    My decimal properties are not money so I have no use for formatting numbers to 2 decimal places (or any fixed number of decimal places) - I just want the numeric value without any trailing zeroes (i.e. 10.5 => "10.5", 0.063 => "0.063", 5.123456 => "5.123456").

    Look at the attached image of my debugger, you'll see that the ToString on the entity property gives a different result than a ToString on a non-entity variable of the same numeric value (in the image below, point is an EF entity and TMP_LONGITUDE is a decimal? property of point).

    So how do I globally remove the "2 decimal place" preference that Entity Framework seems to have added to the decimal.ToString method for me?


    I have created a metadata class for the entity in question and applied a formatting decorator to one property like so:

        public class ProcessMetadata
            [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:g0}")]
            public decimal? THROUGHPUT_OZONE;
        public partial class Process

    That works for that one field but I have dozens or maybe hundreds of decimal fields in the database - any way to to this globally?



    • Edited by KubuliJohn Saturday, August 3, 2013 6:58 PM Update with code example
    Friday, August 2, 2013 11:04 PM


All replies

  • Hello KubuliJohn,

    Thank you for posting in MSDN Forum.

    From your description, I notice the issue you are experiencing is that how to format your decimal value globally.

    With the code provided by you,I think that if we want to reach your goals,we have to use extension method to write ourselves “ToString()” rather than use the default method.

    Here is the link:


    It shows how to use the extension method.

    You can also use the “string.Format()” to format the value.

    It can format date more flexible but it also consumes more resources.

    Here is the link:


    If I have misunderstood anything, please feel free to let me know.

    Could you please tell us the project construction and environment so that we can help you better.

    I look forward to hearing from you.

    Best Regards.

    Fred Bao
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, August 5, 2013 9:20 AM
  • Thanks for the suggestion Fred - it does work but has the unfortunate side effect of preventing me from intentionally formatting a decimal using the ToString method (since that would be extended to force a "g0" format string).  So I would have to create another extension like decimal?.ToStringFormatted(string format) when I do actually want to format something (usually to put it into scientific notation for a report of some kind).

    At present I have added format decorators in metadata classes for all of my decimal fields which is cumbersome but works with no undesirable side effects (yet).

    As an engineer/scientist I wish that there were no implicit assumptions in Entity Framework about what I want to do with my decimal numbers (we're not all accountants)!  Oh well...



    Monday, August 5, 2013 9:44 PM
  • Hello KubuliJohn,

    I am sorry for my being late for your post and the solution I suggested do not help you better.

    And I think what you want is an issue that I will submit to the product R & D Center.


    Thanks for your understanding.

    Fred Bao
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Fred Bao Monday, August 12, 2013 1:17 AM
    Wednesday, August 7, 2013 6:04 AM