title: Asp.Net-Core - OutputFormatter to format decimal result to two decimal places
body: <p>I'm working on implementing a custom output formatter for decimal type properties of a class, it should return two decimal places always. 
  For example: value = 5 , then it should be 5.00.
  I'm creating a custom OutputFormatter in Asp.net Core, however, when I apply the override for CanWriteType, my code method (WriteResponseBodyAsync) is never hit. If I remove that CanWriteType method, then it hits the WriteResponseBodyAsync method, but the context.Object contains all the class, not only the decimal properties of it. </p>
  
  <p>How can I make this OutputFormatter work with all decimal properties in responses?
  Any help will be appreciated.</p>
  
  <pre><code>public class CustomDecimalFormatter : OutputFormatter
  {
      public string ContentType { get; private set; }
      public CustomDecimalFormatter(){
          ContentType = "application/json";
          SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("application/json"));
  
          }
  
      protected override bool CanWriteType(Type type)
      {
          return type == typeof(decimal);
      }
  
      public override Task WriteResponseBodyAsync(OutputFormatterWriteContext context)
      {
          var response = context.HttpContext.Response;
          var decimalValue = (decimal)context.Object;
  
          var formatted = decimalValue.ToString("F2", CultureInfo.InvariantCulture);
  
          using (var writer = new StreamWriter(writeStream))
          {
              writer.Write(formatted);
          }
  
          return Task.FromResult(0);
      }
  }
  </code></pre>