title: How can I force a minimum number of decimal places in Json.net?
body: <p>I'm getting an annoying inconsistency when I'm writing decimals to json using json.net. Sometimes it's to 1 dp, other times 2.</p>
  
  <p>Obviously I'm aware of solutions to output decimals to strings with a certain number of decimals such as <a href="https://stackoverflow.com/a/164932/360211">this</a>, but you don't have that control using json.net without writing a custom serializer I guess.</p>
  
  <p>I am also aware of <code>Math.Round</code> to enforce a <strong>maximum</strong> number of decimal places, this question relates to enforcing a minimum number of decimal places.</p>
  
  <p>The first two tests show what is happening, it is keeping the original number of decimal places from the declaration or calculation.</p>
  
  <p>I found I can add and then subtract a small fraction which the next two tests show working, but is there a cleaner way?</p>
  
  <pre><code>[TestFixture]
  public sealed class DecimalPlaces
  {
      public class JsonType
      {
          public decimal Value { get; set; }
      }
  
      [Test]
      public void TwoDp()
      {
          var obj = new JsonType { Value = 1.00m };
          Assert.AreEqual("{\"Value\":1.00}", JsonConvert.SerializeObject(obj));
      }
  
      [Test]
      public void OneDp()
      {
          var json = new JsonType { Value = 1.0m };
          Assert.AreEqual("{\"Value\":1.0}", JsonConvert.SerializeObject(obj));
      }
  
      private decimal ForceMinimumDp(decimal p, int minDecimalPlaces)
      {
          decimal smallFrac = 1m/((decimal)Math.Pow(10, minDecimalPlaces));
          return p + smallFrac - smallFrac;
      }
  
      [Test]
      public void ForceMinimumTwoDp()
      {
          var obj = new JsonType { Value = ForceMinimumDp(1.0m, 2) };
          Assert.AreEqual("{\"Value\":1.00}", JsonConvert.SerializeObject(obj));
      }
  
      [Test]
      public void ForceMinimumThreeDp()
      {
          var obj = new JsonType { Value = ForceMinimumDp(1.0m, 3) };
          Assert.AreEqual("{\"Value\":1.000}", JsonConvert.SerializeObject(obj));
      }
  }
  </code></pre>