Arndt Bieberstein
Arndt Bieberstein

Reputation: 1128

Custom Json.NET float formatting

I'm using Json.NET for object serialization. I have a lot of float values so my Json string get's very big. I don't need high precision so I was wondering if you could set up a custom float format. For example rounding to 2 decimals.

3.14159265359 => 3.14

The only thing I figured out was a DateFormatHandling using the JsonSerializerSettings. but nothing with floats.

Upvotes: 0

Views: 3882

Answers (1)

Lloyd
Lloyd

Reputation: 29668

Yes you can, you need to code your own custom type converter. See here for an example of a custom converter:

How to implement custom JsonConverter in JSON.NET to deserialize a List of base class objects?

You simply need to adapt it to round up when you write out your float value.

Upvotes: 2

Related Questions