Greg, That still does not make it easy to use with the JSON serializer where my main issue is. Here is an solution I found to work reliably across any value I throw at it. private class JsonTextWriterOptimized : JsonTextWriter { public JsonTextWriterOptimized(TextWriter textWriter) : base(textWriter) { } public override void WriteValue(decimal value) { // we really really really want the value to be serialized as "0.0000" not "0.00" or "0.0000"! value = Math.Round(value, 4); // divide first to force the appearance of 4 decimals value = Math.Round((((value+0.00001M)/10000)*10000)-0.00001M, 4); base.WriteValue(value); } }
Then use the custom writer: var jsonSerializer = Newtonsoft.Json.JsonSerializer.Create(); var sb = new StringBuilder(256); var sw = new StringWriter(sb, CultureInfo.InvariantCulture); using (var jsonWriter = new JsonTextWriterOptimized(sw)) { jsonWriter.Formatting = Formatting.None; jsonSerializer.Serialize(jsonWriter, instance); } On Mon, Aug 12, 2013 at 1:38 AM, Greg Harris <harris.gre...@gmail.com>wrote: > I would have thought that > ? ((decimal)123.45).ToString("0.0000") > "123.4500" > would be cheaper faster more understandable? > > On Sun, Aug 11, 2013 at 6:57 PM, Corneliu I. Tusnea < > corne...@acorns.com.au> wrote: > >> Yes, that's my issue. It seems that if you somehow tell is there are >> multiple zeros is keeps than and displays them during the .ToString(). >> This is what I ended up doing: >> private class JsonTextWriterOptimized : JsonTextWriter >> { >> public JsonTextWriterOptimized(TextWriter textWriter) >> : base(textWriter) >> { >> } >> >> public override void WriteValue(decimal value) >> { >> // we really really really want the value to be serialized as >> "0.0000" not "0.00" or "0.0000"! >> //This is very important for all our hash calculations >> *value = Math.Round(value, 4); * >> * value = Math.Round((((value+0.00001M)/10000)*10000)-0.00001M, 4); // >> divide first to force the appearance of 4 decimals* >> base.WriteValue(value); >> } >> } >> The I use this writer during the serialization. >> >> That will make 123.12 > 123.1200 and even 100 to 100.0000 :) >> >> >> >> On Sun, Aug 11, 2013 at 5:34 PM, Mark Hurd <markeh...@gmail.com> wrote: >> >>> Note that, obviously, one of Decimal's claims to fame is that it >>> considers trailing zeros as significant, so serializing /should/ >>> record those details. >>> >>> If you want to adjust that, use Decimal.Round(value, 2), but note that >>> this does not add trailing zeros, only removes extras. >>> >>> -- >>> Regards, >>> Mark Hurd, B.Sc.(Ma.)(Hons.) >>> >>> >>> On 11 August 2013 14:32, Corneliu I. Tusnea <corne...@acorns.com.au> >>> wrote: >>> > Hi, >>> > >>> > Anyone working today? >>> > >>> > How can I force the NewtonSoft Json Serializer to serialize two >>> decimals the >>> > same way? decimal a = 1234.1200M; decimal b = 1234.12M; >>> > >>> > var sa = JsonConvert.SerializeObject(new { value = a }); >>> > var sb = JsonConvert.SerializeObject(new {value = b}); >>> > Console.WriteLine(sa); >>> > Console.WriteLine(sb); >>> > >>> > Results are: {"value":1234.1200} {"value":1234.12} >>> > >>> > How can I force it to serialize them both with 4 decimals so the >>> results are >>> > identical? >>> > >>> > Even simpler, ignoring the serializer, how can I make sa.ToString() == >>> > sb.ToString() ? >>> > The Json Serializer is only doing a simple .ToString() behind the >>> scenes. >>> > >>> > Regards, >>> > Corneliu >>> > >>> >> >> >