Back

[SOLVED] double attributes are saved as int

  • 0
  • Databases
  • Flutter
hortigado
17 Apr, 2023, 05:40

Hi, I don't know if it's my fault but I save the values as double but they are saved as int.

TL;DR
The user is reporting an issue where double attributes are being saved as int. They suggest converting the values to double or using the 'num' type. The response explains that JSON does not differentiate between int and double, so whole numbers are simplified to drop the decimals. It suggests converting the value to double in the appropriate language or using "json['price']?.toDouble()" in Flutter. The user suggests applying a "toFixed" condition, but it is not possible due to how the data is handled by JavaScript/the browser. They mention that saving values from the console works, but values saved from the project are not saving as double. The support
hortigado
17 Apr, 2023, 05:41

which causes me an error, because I expect a double value

hortigado
17 Apr, 2023, 05:48

As far as I can see, appwrite approximates the closest value, but that's not what I want. Is there any way to disable this approximation?

joeyouss
17 Apr, 2023, 15:09

Hi - let me take a look at this

joeyouss
17 Apr, 2023, 15:52

are you writing values in console?

joeyouss
17 Apr, 2023, 15:55

can you please elaborate a bit about how you are storing values so i can see if this could be a bug?

hortigado
17 Apr, 2023, 16:10

Either way, it doesn't save as a double value. Either saving from the console for example default value or values saved from the project

hortigado
17 Apr, 2023, 16:13

although creating a document from the console does work

joeyouss
17 Apr, 2023, 16:17

we tried replicating this and we are able to make the document with decimal values. Can you elaborate on what are you exactly trying to do (the steps) and/or code piece so we can see what's happening

hortigado
17 Apr, 2023, 16:20

I already found the reason. It is because when we save a value, for example 25.0, it is saved as 25. But if 25.1 is saved if the double value is respected.

hortigado
17 Apr, 2023, 16:22

but this generates problems for me because I would have to be converting the values obtained to double

joeyouss
17 Apr, 2023, 17:02

Tagging @Torsten Dittmann here

Torsten Dittmann
17 Apr, 2023, 17:04

This might be an interesting issue. I am pretty sure on the backend its handled properly, but Javascript/the browser uses the same data type for both int/float values.

So it is properly saved into the backend, but the moment Javascript gets it from the Database, it makes it an integer. On many other languages it should be interpreted correctly.

hortigado
17 Apr, 2023, 17:32

Mmm I understand. And it is not possible to make a condition .endsWith(".00") and if it is fulfilled, apply a toFixed?

hortigado
17 Apr, 2023, 17:37

or the best solution would be to convert the values obtained from the database back to double? although it should be an unnecessary task

Drake
17 Apr, 2023, 20:41

In flutter, you'll need to convert/cast it to a double

Drake
17 Apr, 2023, 20:45

From the Appwrite side, it is expected to return something like:

TypeScript
{
  "price": 333
}

JSON is JavaScript and JavaScript does not differentiate between int or double. As such, whole numbers will be simplified to drop the decimals.

In a language that differentiates the two, you'll have to convert it accordingly. For example, you can do json["price"]?.toDouble().

Drake
17 Apr, 2023, 20:48

or you can use the num type

hortigado
17 Apr, 2023, 23:27

Ok friend thanks, if there is no other way I will choose to convert the values to double 👍

hortigado
17 Apr, 2023, 23:27

[SOLVED] double attributes are saved as int

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more