Contentful logo

Contentful Community

Integers returned as Double

#1

Even though an attribute in a Content Type is defined as Integer, when it is queried it is returned as a Double. Therefore, I need to cast it to Double and then convert it to Integer. The resulting code, in Java, is like this entry.getField[java.lang.Double]("propertyReferenceID").toInt.
Is there any reason why this happens?

0 Likes

#2

Seems to work just fine for me…

If I get https://cdn.contentful.com/spaces/<my-space-id>/content_types/<my-content-type-id>?access_token=<my-delivery-token> it returns this:

{
...
  "fields": [
...
    {
      "id": "price",
      "name": "Price",
      "type": "Number",
      "localized": false,
      "required": false,
      "disabled": false,
      "omitted": false
    },
...
    {
      "id": "quantity",
      "name": "Quantity",
      "type": "Integer",
      "localized": false,
      "required": false,
      "disabled": false,
      "omitted": false
    },
...
  ]
}

One is a Number and one is an Integer. Then if I get an entry of that content type like this: https://cdn.contentful.com/spaces/<my-space-id>/entries/<entry-id>?access_token=<my-delivery-token> I get this:

{
...
  "fields": {
...
    "price": 12.99,
...
    "quantity": 123
  }
}

Do you have your field types set correctly? Can you do the requests like I showed and share the responses?

0 Likes

#3

Hello everyone,

thanks Charlie for the API answer, let me shine some light on the issue from the Java SDK perspective:

Since the Integer type in Contentful is defined as a Number type without decimals. Values from -2^53 to 2^53 this will not fit into a Java Integer (ranging from Integer.MIN to Integer.MAX). The closest fit we could find is Double, and therefore chose it to be used everywhere.

I hope that clarifies the decision, and your solution would be the preferred one.

Greetings,
Mario

0 Likes

#4

It makes sense.
Thank you for your reply.

0 Likes

#5

@mario thanks for the reply! Contentful Number can be of two types: Integer and Decimal. In my opinion Integer which can get to 2^53 should be mapped to Long instead of Double which represents a decimal value.

0 Likes