Greg,

Using WriteValue (decimal) to write 123.4567 will output { value: 123.4567 }
Using WriteValue(string) to write (123.4567).ToString() will output {
value: "123.4567" } so will consider the element a string which is not what
I want.

I really want the numbers to be all formatted in a consistent way as I'm
actually using the serialization to check a change hash on the object to
see if any value was changed.


On Mon, Aug 12, 2013 at 9:36 AM, Greg Harris <g...@harrisconsultinggroup.com
> wrote:

> Hi Corneliu,
>
> I based my thoughts on my assumption that the WriteValue(decimal) method,
> would need to output a string representation of the decimal value.  The
> documentation (
> http://james.newtonking.com/projects/json/help/index.html?topic=html/Methods_T_Newtonsoft_Json_JsonTextWriter.htm)
> does not help at all here.  Looking at where you call the base
> WriteValue(decimal) method I would have thought that you could call
> WriteValue(string)?
>
> So:
>     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);
>     }
>
> Would become:
>     public override void WriteValue(decimal value)
>     {
>         string result = value.ToString("0.0000");
>         base.WriteValue(result);
>     }
>
> WARNING: This solution is NOT tested!
>
> Regards
> Greg H
>
> On Mon, Aug 12, 2013 at 9:03 AM, Corneliu I. Tusnea <
> corne...@acorns.com.au> wrote:
>
>> 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
>>>>> >
>>>>>
>>>>
>>>>
>>>
>>
>

Reply via email to