Strange behavior with numbers

Justin Stanley 0 Reputation points
2024-07-26T16:06:18.4733333+00:00

Hello,

 

We’ve been using cosmos for a few years now and just recently discovered some odd behavior. We are using the .net SDK but occasionally use the data explorer to manually view/update data for production support purposes.

 

To illustrate this odd behavior, we’ll use the following .net class

 

    public class Foo

    {

        public int MyInt { get; set; }

        public double MyDouble { get; set; }

        public decimal MyDecimal { get; set; }

    }

 

 

Now, in .net if I set them all to 0, and stuff them into my cosmos container, like so:

 

            var foo = new Foo

            {

                MyInt = 0,

                MyDouble = 0,

                MyDecimal = 0

            };

 

_container.CreateItemAsync(foo)

 

When we retrieve the item from cosmos .net client MyDouble and MyDecimal return as 0.0.

When we retrieve the item using an HTTP request in postman, MyDouble and MyDecimal return as 0.0.

The kicker is, when we retrieve the item using the data explorer, MyDouble and MyDecimal return as 0.

 

Additionally, when we update the item using data explorer, the values for MyDouble and MyDecimal are saved as 0 and returned to .net client as C as well as the HTTP/REST API in postman.

 

My understanding is the .net SDK is simply making an HTTP POST sending the serialized json to cosmos. I have inspected the request message being sent by the .net sdk, and confirm it is sending 0, not 0.0.

 

It seems cosmos under the hood somewhere is persisting the data as 0.0, vs 0, but the data explorer is taking the data as it was typed in by the user.

 

Is there any insight you can provided into why we are seeing this behavior?

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,612 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Azar 22,860 Reputation points MVP
    2024-07-26T19:33:58.4933333+00:00

    Hi there Justin Stanley

    Thanks for using QandA platform

    I think thee behavior you're seeing is likely due to how different tools handle numeric values in JSON. like when you store values in Cosmos DB using the .NET SDK, double and decimal types are serialized as 0.0. but, the Cosmos db Data Explorer may display these as 0 due to JavaScript’s handling of numbers, which doesn’t distinguish between 0 and 0.0. i suggest to implement custom JSON converters in .NET to force double and decimal serialization to always include the decimal point.

    If this helps kindly accept the answer thanks much.


  2. Sai Raghunadh M 150 Reputation points Microsoft Vendor
    2024-08-27T03:14:14.5166667+00:00

    Hi @ Justin Stanley,

    Thanks for the question and using MS Q&A platform.

    When you use the .NET SDK to send numbers to Cosmos DB, the SDK sends them as a special type of number called a "double-precision floating-point number". This type of number can have decimal places, even if you don't see them.

    Because of this, when you send the number 0 using the SDK, it is actually sent as 0.0. This is because the SDK always sends numbers as double-precision floating-point numbers by default.

    If you want to check this, you can use a tool like Fiddler to see the exact request that is being sent to Cosmos DB. This will show you that the number is being sent as a double-precision floating-point number.

    To avoid this issue, you can try by sending string instead of sending the numbers that does not use double-precision floating-point numbers. This helps you manage how numbers are sent to Cosmos DB and avoid extra decimal places.

    Hope this helps. Do let us know if you have any further queries.


    If this answers your query, do click Accept Answer and Yes for was this answer helpful.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.