-2

I am returning a decimal like '0.00000004177109' on API. When i prepare response on API Controller it is converting this decimal to it's scientific notation "4.177109e-8". This happens when i read response's content with ReadAsStringAsync or ReadAsStreamAsync , etc. I am getting same when i convert it to double. By the way , i am using Newtonsoft.JSON in json operations. Is there anybody else having same issue like this before ?

Here is the code snippet.

           var response = task.GetAwaiter().GetResult();


            var dd = response.Content.ReadAsStreamAsync().GetAwaiter().GetResult(); //When it reads , it is changing to scientific notation
            StreamReader readStream = new StreamReader(dd);
            var str = readStream.ReadToEnd();

            str = str.Replace(); //Replacing something in readed content.

            response.Content = new JsonContent(str);

            return response;
  • 3
    4.177109e-8 its shorten way to write of 0.00000004177109. – styx Jun 01 '20 at 07:06
  • Because the api is serialising a double? – Jeremy Lakeman Jun 01 '20 at 07:09
  • @styx Hi. I know it is shorten way. When target system tries to get it they are getting error because this field is decimal in their system. – Cemal Çelik Jun 01 '20 at 07:20
  • @JeremyLakeman This field's data type is decimal in my Class. – Cemal Çelik Jun 01 '20 at 07:21
  • 2
    Please share a [mcve]. – mjwills Jun 01 '20 at 07:24
  • You haven't posted any client or server code that describes the types you expect. str is just a string. Where is the code that generates that string? – Jeremy Lakeman Jun 01 '20 at 07:34
  • @CemalÇelik "their system"? who are they are, I still don't understand your problem – styx Jun 01 '20 at 07:56
  • @JeremyLakeman I don't know their client side code actually. I can share code snippet from my side and will be do it in a minute. – Cemal Çelik Jun 01 '20 at 08:03
  • @styx Some of our customers are getting detailed information from our system via API. And in our system a method gets XML and reads it. Then it sends to API Controller to prepare response. Then in controller, code snippet i shared in question reads it and adds something to response thats why i converted it to string and then creates new JsonContent to send the response. – Cemal Çelik Jun 01 '20 at 08:22
  • @CemalÇelik so what is the problem actually? – styx Jun 01 '20 at 08:52
  • @styx Issue is , API has to return decimal like it is original value(0.00000004177109) not scientific notation (4.177109e-8) of it. How to get rid from it ? – Cemal Çelik Jun 01 '20 at 08:58
  • @CemalÇelik but those numbers are the same, just written diffrently – styx Jun 01 '20 at 09:01
  • @styx i know. Its just scientific notation of this decimal. I think they're deserializing JSON and directly sets to database or anywhere else. The place they're setting is declared as Decimal. Thats why this is happening. Is there a way to get rid from this scientific notation ? – Cemal Çelik Jun 01 '20 at 11:07
  • I was able to reproduce the behavior you see when serializing `double` values, not `decimal` values. See: https://dotnetfiddle.net/rruJWd which shows that scientific notation is sometimes used when serializing `double` but not `decimal`. However, this is not a violation of the JSON standard as JSON does allow for scientific notation for floating point numbers, see https://www.json.org/json-en.html. Is there any chance you could use `decimal` in your model, or get the receiving system to fix their JSON parser? – dbc Jun 01 '20 at 15:28
  • I could probably provide you with an appropriate `JsonConverter` if you can share a [mcve] showing exactly where the unwanted (though still well-formed) scientific notation is being emitted. – dbc Jun 01 '20 at 16:11
  • @dbc Hi. Thanks for your reply. This field is decimal in my model. But as i debugged , issue occurs when i am setting dd with reading content. It seems it's not an issue with JSON. I made a test project for this and did the same thing as you and it is serializing exactly same value. – Cemal Çelik Jun 01 '20 at 16:13
  • Then you might want to [edit] your question and update the code and tags to show exactly where the problem is and how to reproduce it, I don't know what you mean by *issue occurs when i am setting dd with reading content*. `DoubleConverter.ToExactString()` from [this answer](https://stackoverflow.com/a/24955729/3744182) by [Jon Skeet](https://stackoverflow.com/users/22656/jon-skeet) may prove helpful. – dbc Jun 01 '20 at 16:18
  • @dbc I just edited the question. – Cemal Çelik Jun 01 '20 at 16:25
  • Without knowing the contents of `str` we can't really help you. Knowing the exact framework you are using (for `JsonContent`) would increase the chances of getting help also. See: [ask]: *Help others reproduce the problem... Include just enough code to allow others to reproduce the problem.* In this case the JSON string would be needed. – dbc Jun 01 '20 at 16:38

1 Answers1

0

This is considered normal behavior as it makes the json smaller in size. Potentially reducing bandwidth usage.

Deserializers do understand the scientific notation. When a deserializer reads "4.177109e-8" it wil interpret it as 0.00000004177109. So dont worry about it.

Martijn
  • 739
  • 9
  • 26