0

I need to convert German format currency in a JSON (value may be negative or positive) to Double, I am able to do it using double.parse and specifying NumberStyle, something like below

CultureInfo culture = CultureInfo.CreateSpecificCulture("de-DE");
var convertedValue = double.Parse("2.664.221,01-", NumberStyles.Currency, culture);

But, there are multiple properties that require conversion and I am looking for a better way to do it instead of doing parsing for each property. I am thinking of using JsonSerializerSettings and then use Json Convert. But could not find a way to specify styles in JsonSerializerSettings.

Is there a better way to handle this?

var settings = new JsonSerializerSettings()
                    {
                        Culture = new CultureInfo("de-DE"),
                        //NumberStyle = currency 
                    };

                    var res = JsonConvert.DeserializeObject<MyObject>(json, settings);
  • This is by design, `2.664.221,01-` is a malformed JSON number according to the JSON spec https://www.json.org/json-en.html. See [German culture - get double number from JSON with a comma](https://stackoverflow.com/q/28405066/3744182). (`JsonSerializerSettings.Culture` mainly affects `DateTime` serialization I believe, as there's no standard for how dates & times are represented in JSON.) – dbc Feb 12 '20 at 07:35
  • Hypothetically you could serialize all floating point numbers as strings rather than as numbers, and if you did you could format them in the German locale, e.g. `"2.664.221,01-"` If that's what you want, see [How to localize when JSON-serializing?](https://stackoverflow.com/a/20777168/3744182) and [Handling decimal values in Newtonsoft.Json](https://stackoverflow.com/q/24051206/3744182). In fact I'd say your question is a duplicate of those three questions. – dbc Feb 12 '20 at 07:36

0 Answers0